- Home /
Character Face Camera Direction
Hi
I have written a script that makes my character move. However, I would like the character to face which ever way the camera is pointing
Here is the script:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; var distance : float = 5;
private var moveDirection : Vector3 = Vector3.zero;
function Update() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
I have tried using
moveDirection = transform.LookAt(transform.position + Vector3(Camera.forward.x, 0, Camera.forward.z));
to make the character face the direction of the camera but no luck :(
Any help would be amazing!
"no luck" == compiler error? What is the result of your script?
Answer by DaveA · Mar 19, 2012 at 11:44 PM
Assuming this script is on the character, try this:
transform.rotation = Camera.main.transform.rotation;
It's cool I sorted it :D
But thank you for the reply anyway :)
This doesn't work for me. I'm using the controlling script from the "roll a ball" tut.
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce (movement * speed * Time.deltaTime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613065319im_/https://answers.unity.com/themes/thub/images/avi.jpg)