x


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!

more ▼

asked Mar 19, 2012 at 10:12 PM

Joevis gravatar image

Joevis
1 39 26 26

Is this script on the character or the camera?

Mar 19, 2012 at 11:43 PM DaveA

"no luck" == compiler error? What is the result of your script?

Mar 19, 2012 at 11:46 PM Dakwamine
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Assuming this script is on the character, try this:

transform.rotation = Camera.main.transform.rotation;

more ▼

answered Mar 19, 2012 at 11:44 PM

DaveA gravatar image

DaveA
33.6k 419 346 487

It's cool I sorted it :D

But thank you for the reply anyway :)

Mar 19, 2012 at 11:45 PM Joevis

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);

 }
Aug 26 at 07:43 PM Lunas Lama
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5057
x1545
x517
x103

asked: Mar 19, 2012 at 10:12 PM

Seen: 1794 times

Last Updated: Aug 26 at 07:43 PM