I’m trying to ray cast from the centre of an enemy to my player and I’ve been following a tutorial that explains that ray casting happens from the pivot point of the object with the script on it.
Since the pivot point of my enemy character is at its feet, the tutorial says the following code moves the ray cast up 1 “unit” which would be approximately the midpoint of the character in the tutorial because that character is 2 units. Mine is a lot smaller and thus the whole easy peasy unit thing doesn’t work. this is the line of code to move the ray cast up one unit:
if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
I either need to move my pivot point of the enemy character or i need to know how to write the code pasted above in such a way that i can control how high up the ray cast is transformed. help would be very welcome
Transform.up and Vector3.up are not the same thing, and if you’re going to add to the current position you need to use Vector3.up. You can take Lorinthio’s approach and make a new Vector3 with the “y” being the height value you wish, or you just multiply the Vector3.up by the percentage height difference, like:
if(Physics.Raycast(transform.position + (Vector3.up * height), transform.forward, out hit, col.radius))
or something. If you want to know the difference between Vector3.direction and Transform.direction, first check the difference between local and world space coordinates, and maybe look at the manual page for Transform.TransformDirection too. Transform.up, for instance, is equivalent to Transform.TransformDirection(Vector3.up), but shorthand.