behnder
December 10, 2021, 1:12am
1
Hello all. I need your help with some practice with raycast , I have this little piece of code in a capsule with a navmeshAgent. But when I run my script I just have the first collision of the ray, like if i running that piece of code just one time. If it is in the update, doesn’t mean that all time I’ll have the collider name of the things in front of me?(if I’m moving around the enviroment of course). I trying moving my capsule manually around the enviroment but I just have the first ray collision.
public class EstadoPerseguir : MonoBehaviour
{
private MonoBehaviour Perseguir;
private MonoBehaviour EnemigoEnRango;
bool isHitting = true;
Ray ray;
[SerializeField] private GameObject enemy;
RaycastHit hit;
private void Awake()
{
ray.origin = transform.position;
ray.direction = transform.forward;
}
private void Update()
{
Physics.Raycast(ray.origin, ray.direction, out hit);
print(hit.collider.name);
if (isHitting)
{
Debug.Log("HITTING " + hit.collider.name);
}
}
}
This is in front of a “Cube”
This is in front of a “Player”
Thanks in advance!
Check this critical C# piece of knowledge out:
The difference between Value Types vs Reference Types:
"Object" is a loose term, usually used to refer to a thing that has more than one item inside, but objects can even have ZERO things inside them, such as an empty class or interface with no methods.
And technically at some level everything is an object in some sense. Like an int is actually a handy alias for a System.Int32... same exact animal. Hover over an int and see:
[6779456--784505--Screen Shot 2021-01-29 at 6.37.46 AM.png]
All those interfaces are also implemented for…
What you do in Awake() is creating a copy of those values, frozen in time.
Move the two lines in Awake to right before you do the cast.
ALSO: Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:
Physics.Raycast() is a HORRIBLY overloaded function. I recommend ALWAYS specifying the named arguments rather than hoping you got them in the correct order.
This is because layerMask (an int) and maxDistance (a float) can be easily mistaken at the call site if you are passing an integer value for maxDistance. In this case, no error will be returned, and it will just fail mysteriously.
The time you save by typing out named arguments will be your own.
Instead of this:
Physics.Raycast(origi…
behnder
December 10, 2021, 1:57am
3
Kurt-Dekker:
Check this critical C# piece of knowledge out:
The difference between Value Types vs Reference Types:
https://discussions.unity.com/t/826396/4
What you do in Awake() is creating a copy of those values, frozen in time.
Move the two lines in Awake to right before you do the cast.
ALSO: Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:
https://discussions.unity.com/t/758115/8
I reaaally appreciate the help man, and the explanations. It works, was just that.
Thank you!!
1 Like