- Home /
Checking in front of an object before moving
Hi, I have a FPS character script with movement and jumping. Jumping works good, but the movement could be improved. Here's the part of the script in question:
if (!Physics.Raycast(transform.position,Vector3(0,0,transform.rotation.z),10)){
var z = Input.GetAxis("Vertical")*speed*Time.deltaTime;
}
The problem is when you're walking into a wall, the player will have a seizure until finally climbing on top of the wall/floor in front of it. What I'm trying to do to counter this is check for a collision in front of the character before moving. It doesn't work however, it still has the same behavior. I've tried a couple other variants of the code, such as using transform.rotation.z instead of Vector3(blah), but it gives be an wrong argument type error(correct name?). Would anyone mind helping me please?
P.S. Call me lazy, but the player has a rigidbody attached to it to handle the gravity and collisions. P.P.S I'm making this code up from stuff found on the scripting reference, but I don't entirely understand it all though...
Answer by Flynn · Jun 27, 2011 at 04:18 AM
Physics.Raycast does not take a rotation, it takes a direction. (1, 0, 0) is a direction that points in the X axis direction, but is not rotated 1 degree on the x axis. (0.5, 0.5, 0) would be a diagonal cross between the X and Y axis directions.
Try replacing your Vector3 with:
transform.TransformDirection(Vector3.forward)
TransformDirection will take a direction in local space relative to the transform, then convert it to global space
I really should start remembering that there's a Transform.Direction. Thanks for helping!