x


Camera and Character rotation

My aim is to get the character rotating left/righ when moving the cursor on the x axis, and the camera rotating up/down when moving the cursor on the y axis. I made the camera a child to my character and attached this code to the character.

var speed = 4.0;

function Update () {

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    var hitdist = 10.0;

    //Character rotation

    var targetPoint = ray.GetPoint(hitdist);

    targetPoint.y = transform.position.y;

    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

    //Camera rotation

    targetPoint = ray.GetPoint(hitdist);

    targetPoint.x = Camera.main.transform.localPosition.x;

    targetRotation = Quaternion.LookRotation(targetPoint - Camera.main.transform.position);

    Camera.main.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);

}

At the moment the left/right rotation is working fine but the camera isn't rotating at all, and it's very fidgety for some reason. Would appreciate any tips.

more ▼

asked Jan 14 '11 at 12:39 PM

Rikus gravatar image

Rikus
3 2 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It looks to me like you are creating a ray with the mouse position... is there any reason you didn't just use the mouse movement? It seems much simpler to just:

var moveSpeed:float = 10.0;


function Update(){

    var moveUP:float = Input.GetAxis("Mouse Y")* moveSpeed * Time.deltaTime;
    var moveSide:float = Input.GetAxis("Mouse X")* moveSpeed * Time.deltaTime;

    Camera.main.transform.Rotate(moveUP,moveSide,0);

}
more ▼

answered Jan 14 '11 at 01:26 PM

The_r0nin gravatar image

The_r0nin
1.4k 9 14 30

(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:

x3013
x1047
x87

asked: Jan 14 '11 at 12:39 PM

Seen: 1319 times

Last Updated: Jan 14 '11 at 12:39 PM