- Home /
Raycast in Direction of Input Movement
I have a vector3 MoveDirection X = Horizontal Input Z = Vertical Input
I want to fire a ray, from the player towards the direction the player is moving. Then if this ray hits the "Move Toward" object I can return true.
Possible example
Vector3 v = new Vector3(moveDirection.x, 0, moveDirection.z);
RaycastHit hit;
if (Physics.Raycast(player.transform.position, v, out hit, 5)
{
if(hit.collider.tag == "ObjectWeWantToMoveToward")
{
//we must be moving toward it as our move direction ray is pointing at it
return true;
}
else
{
//we must not be moving toward it as our move direction ray is not pointing towards it
return false;
}
}
The obvious issue here is that our vector3 of V is not actually the direction we want to point the ray to. So how do I properly fire the ray in the correct direction?
Thanks!
Nothing stands out in this code as problematic, I am not sure why v wouldn't be the direction you're moving.
My only suggestions are could the move direction you're receiving be local to your player object, if your player is a child of another object and this movement sets its LOCAL position then this could be the case and to get the global space move direction then you'll want to use the "TransformDirection" function on your player's transform: v = player.transform.TransformDirection(v);
If you player always faces the direction they're facing you could use player.transform.forward
as your ray direction?
I know comparing strings can have certain caveats where two strings of equal content do not return true when compared with the == operator, so use .Equals or in this case you can use the CompareTag function on gameobject: hit.collider.gameObject.CompareTag("ObjectWeWantToMoveForward")
These doesn't seem too likely though. To debug I'd really recommend using Gizmos as it's super hard to tell what's happening in 3D space from just logs of Vector3s. If you haven't used them, gizmos get drawn in the OnDrawGizmos function:
private void OnDrawGizmos()
{
Gizmos.color = Color.magenta;
Vector3 v = new Vector3(moveDirection.x, 0f, moveDirection.z);
Gizmos.DrawRay(player.transform.position, v);
}
Then in the scene view you'll be able to see the direction of your ray as a magenta line, then move your player and see if you can see any patterns in the line's behaviour.
Let me know if I've misunderstood anything about your question! If none of this is helpful then post the code where you apply that movement to your player's transform.
The game is an FPS so x/z will not always be the same direction, as the player can rotate. X/Z in Movement will always be -1, or 1. As they're inputs.
Currently I'm firing a ray toward the rigidbody velocity, as this is the correct direction we are going. However since my velocity basically stops when I hit a wall. this solution won't work.
I figured it out. I was calling the wrong transform, as this script is in a third party.... my mistake. The standard transform.TransformDirection solution works.
When in doubt, visualize your raycasts.
if( moveDirection.sqrMagnitude>0 )// prevents raycast when stationary
{
const int physicsLayerMask = 1<<0;// 1<<0 means Default layer only
const float rayMaxDistance = 5f;
var ray = new Ray{
origin = player.transform.position ,
direction = new Vector3{ x=moveDirection.x , z=moveDirection.z }
};
if( Physics.Raycast( ray , out var hit , maxDistance:rayMaxDistance , layerMask:physicsLayerMask ) )
{
// hit
Debug.DrawLine( ray.origin , ray.origin + ray.direction.normalized*hit.distance , Color.red );
}
else
{
// miss
Debug.DrawRay( ray.origin , ray.direction , Color.yellow );
}
}
Your answer
Follow this Question
Related Questions
Why multiply a vector by -2? 1 Answer
Shoot Raycast to a specified Angle 0 Answers
How to get an offset of a RaycastHit? 0 Answers
raycast and trigonometry 2 Answers
Shotting Or thrwoing Object wtih Vector3.MoveTowards 1 Answer