x


Click a cube to move player over to that position & rotate player to face fixed point

  • what i want to happen is that you see a cube, click it, the First person Controller jumps over there and whilst thats happening, it rotates around to point at a fixed object in the distance

I copied and altered some of Duck's code from the question: 'Click 3D objects (markers) in the distance, causing the camera to snap to that location'. My script added to the cube works BUT the camera ends up facing the wrong way when the script is over. Whats the fix?

script for the cube:

function OnMouseDown() {
var player = GameObject.FindWithTag("Player");
var startTime = Time.time;
var startPos = player.transform.position;
var startRot = player.transform.rotation;
var targetPos = transform.position;
var  targetRot = transform.rotation;


targetPos.y = player.transform.position.y;  // preserve cam's height

while (Time.time < startTime+1) {
    var i = (Time.time - startTime);
    player.transform.position = Vector3.Lerp(startPos, targetPos, i);
    player.transform.rotation = Quaternion.Slerp(startRot, targetRot, i);

    yield;

}

}

The cube also needs another script which points it towards the fixed point:

var target : Transform; 

function Update () { transform.LookAt(target); }

  • any suggestions gladly received and also any links to good scripting tutes!

thanks!

more ▼

asked Apr 17 '10 at 08:44 PM

bruno martelli gravatar image

bruno martelli
135 11 13 24

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

1 answer: sort voted first

The problem could be the Quaternion.Slerp(startRot, targetRot, i). This statement change player's rotation to the rotation of the object (cube here). Vote me up so that I know you still need help on it.

more ▼

answered Mar 07 '12 at 12:33 PM

aaronshan gravatar image

aaronshan
21 1 1 1

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

x3132
x1124
x355
x232

asked: Apr 17 '10 at 08:44 PM

Seen: 2210 times

Last Updated: Mar 07 '12 at 12:33 PM