#Raycast Detection

1 messages · Page 1 of 1 (latest)

dawn martenBOT
#

Before others can help, a clear question must be formulated

When you ask a difficult question it is your responsibility to ensure that anyone reading it will have all of the information they need to understand and diagnose the problem. Sometimes questions aren’t as clear to others as they could be or they may be missing critical information needed to provide a correct answer.
Source: https://idownvotedbecau.se/unclearquestion

Please elaborate on your question by including all the relevant information such as:

  • The programming language within which you're working (if it is anything other than C#)
  • Exactly what it is you're trying to accomplish
  • Things you have considered/attempted already
  • Anything else that could aid answerers in resolving the issue
elder ridge
#

I want to use Raycast using Constructor
These two script i am using .
on Monobeaviour it works fine!

SCRIPTS


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test
{
private Transform playerPosition;
private float rayDistanceFromFloor = 0;
private LayerMask floorMask;

public Test(float rayDistanceFromFloor, Transform playerPosition, LayerMask floorMask)
{
    this.rayDistanceFromFloor = rayDistanceFromFloor;

    this.playerPosition = playerPosition;
    this.floorMask = floorMask;

}




public void PlayerStandinginfo(Vector3 dir,RaycastHit hit)
{
   



    if (Physics.Raycast(playerPosition.position, dir, out hit, rayDistanceFromFloor))
    {
        Debug.Log("Hit the floor" + hit.point);

    }




}

}
//------------------------------------------------------------------
uusing UnityEngine;

public class OtherScript : MonoBehaviour
{
[SerializeField] private Transform playerPosition;
[SerializeField] private float rayDistanceFromFloor = 0;
[SerializeField] private LayerMask floorMask;
[SerializeField] private Vector3 dir;
RaycastHit hit;
private Test testInstance;

private void Start()
{
    
    testInstance = new Test(rayDistanceFromFloor, playerPosition, floorMask);

   
   
}
public void Update()
{
    testInstance.PlayerStandinginfo(dir,hit);
}
private void OnDrawGizmos()
{


    Gizmos.DrawLine(playerPosition.position, playerPosition.position + dir * rayDistanceFromFloor);


}

}

teal sleet
#

I want to use Raycast using Constructor
Why?
Also, you're not even firing the raycast in the constructor.

These two script i am using
You pasted the same script twice, both times without using code blocks, which makes this very hard to read.

on Monobeaviour it works fine!
Then why did you make a wrapper class around the raycast logic?

elder ridge
teal sleet
#

Single raycasts are rarely sufficient for proper collision detection on characters. Especially if you are working in 3D.
But this still doesn't explain you saying that you want to use the raycast from your constructor. Or why you need the wrapper class in the first place.

elder ridge
#

for eg : I have two script one for raycast other for keyboard controller .
.i.e in Keyboard controller i want to move player ground to right for that particular action I need raycast to activate according to press key direction.

I tried prev pass function through Interrface but it didn't work !
So it thought using wrapper class!

Sorry but I fell messy how to create design for code when multiple things communicate , I have to experiment !
which is working and why it is not ?

teal sleet
#

which is working and why it is not ?
I don't know why it's not working.
Perhaps your dir value is incorrect. Or your floorMask is not set up properly. Or playerPosition does not have the value you expect.
Or maybe you have hidden log messages in your console (because that's currently the only way you'll determine whether your ray hit something).

design for code when multiple things communicate
It's good that you want to keep your code separate. But in this case, it won't do you any favours.
Besides, you're not even communicating; You don't pass the result of Physics.Raycast back to OtherScript. And you also don't return (or do anything, really) with the hit parameter of PlayerStandinginfo().