- Home /
Look rotation facing vector zero
My character keeps looking at vector zero when I use the following script, rather than the direction it's moving in. Any suggestions?
if(charController.isGrounded == true) { // Calculate the movement direction (forward motion) //Move in the direction based on the negative of the horizon axis moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); transform.forward = moveDirection;
if(Input.GetButtonDown("Jump")){
///Insert Jump here(Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
//audio.PlayOneShot(Jump);
}
}
//Apply gravity every frame
moveDirection.y -= gravity * Time.deltaTime;
//Move using the direction and walkspeed multiplied by Time.deltaTime
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
}
You're gonna have to break it down more than that...I'm an Idiot here dude.
Answer by Loius · Dec 13, 2010 at 07:31 PM
You need to use instead of "transform.foward = moveDirection":
if ( moveDirection != Vector3.zero )
transform.rotation = Quaternion.LookRotation( moveDirection );
You can replace the .rotation = line with the one you have, but remember that you won't be able to directly control the up-vector as easily (LookRotation defaults to trying to align the object's +Y axis to the world's +Y axis)
Actually, it's not read-only. It would throw an error if it was.
No change. It acts the same as the transform forward. The character will move and rotate to the direction but just snap back to the forward when I let go.
Oh! It snaps back? You need to only apply the rotation change when the input is non-zero. Use "if ( moveDirection.sqr$$anonymous$$agnitude > 0 )"
Answer by Proclyon · Dec 13, 2010 at 06:19 PM
You didn't tell him to look anywhere so he's not going to.
To do that you need to fiddle with the rotation property of the character.
It is stored in the transform.rotation.
For example rotation over Y axis 90 degrees would make your character face absolute right (not relative right).
If it's a 2D world like mario and you can only face absolute left and right Y -90 (or 270) or any increment or decrement of that with 360 (which is pointless but possible) will make your character face left or right.
In a 3D world however you need to do a lot more. So Please do tell what kind of world we're in.
Answer by Peter G · Dec 13, 2010 at 08:23 PM
I would have the character look at its own velocity. Then it always face the direction its moving. Like the CameraRelativeController.js
in the Penelope tutorial.
function FaceMovementDirection() { var horizontalVelocity : Vector3 = charCharacter.velocity; horizontalVelocity.y = 0; // Ignore vertical movement
// If moving significantly in a new direction, point that character in that direction
if ( horizontalVelocity.magnitude > 0.1 )
transform.forward = horizontalVelocity.normalized;
}
P.S. I haven't really used this tutorial for much other than ideas for future projects so I really don't know how well this works, just that I picked it out as logical, and I don't know why Unity would steer you wrong.
Answer by Design3.com · Dec 13, 2010 at 08:10 PM
1) Delete or comment out "transform.forward = moveDirection;" from the script
2) Add the Mouse Look script to your player game object from Component-->Camera Control-->Mouse Look
Vicenti's right; transform.forward is read-only and can be used to find out what direction an object is facing. By setting "moveDirection" and using CharacterController.Move() with that Vector3 value, you're doing all you need to.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090352im_/https://answers.unity.com/themes/thub/images/avi.jpg)