Moving from one place to another.

I’m working on a simple Hockey games where the player moves around five places to shoot a puck at the goal. I’m looking for a way to make smooth transition between the five locations when the ‘a’ key is hit to go left or the ‘b’ key is hit to go right.

var playerposition = 3;

function Update () { 
   	if (Input. GetKeyDown ("a")){ 
		playerposition = playerposition - 1;
			if (playerposition == 0){
				playerposition = 5;
			}	
		}
	else if (Input. GetKeyDown ("b")){ 
		playerposition = playerposition + 1;
			if (playerposition == 6){
				playerposition = 1;
			}	
		}


		 if (playerposition == 1){
			transform.position = Vector3(-418.2407, 60, 315.9193);
			transform.eulerAngles = Vector3(0, 46.48776, 0);
		}
		else if (playerposition == 2){
			transform.position = Vector3(-257.0427, 60, 149.6062);
			transform.eulerAngles = Vector3(0, 21.87396, 0);
		}
		else if (playerposition == 3){
			transform.position = Vector3(-3.386639, 60, -26.3466);
			transform.eulerAngles = Vector3(0, 0, 0);
		}
		else if (playerposition == 4){
			transform.position = Vector3(340.5251, 60, 142.9057);
			transform.eulerAngles = Vector3(0, 329.7681, 0);
		}
		else if (playerposition == 5){
			transform.position = Vector3(402.8006, 60, 334.6724);
			transform.eulerAngles = Vector3(0, 311.4727, 0);
		}
	}

I’m guessing that I could somehow use lerp or slerp but I’m not sure how to do it with this example.

Thanks for your thoughts.

I poked around the forums and found a few posts about lerp so I made some modifications to my script. It doesn’t really work for position and the script generates an error for the rotation.

Any thoughts or corrections?

var playerposition = 3;
var aPosition : Vector3; //new position
var bPosition : Vector3; //new rotation
var cPosition : Vector3; //current position
var dPosition : Vector3; //current rotaion
var damping = 3.0; 

function Update () { 
   	if (Input. GetKeyDown ("a")){ 
		playerposition = playerposition - 1;
			if (playerposition == 0){
				playerposition = 5;
				StartCoroutine ("PlayerMoveSetUp");
			}	
		}
	else if (Input. GetKeyDown ("b")){ 
		playerposition = playerposition + 1;
			if (playerposition == 6){
				playerposition = 1;
				StartCoroutine ("PlayerMoveSetUp");
			}	
		}
	transform.position = Vector3.Lerp(cPosition, aPosition, Time.deltaTime * damping);   
	transform.rotation = Quaternion.Slerp(dPosition, bPosition, Time.deltaTime * damping);  //this line causes the error  
	}

function PlayerMoveSetUp () {

		cPosition = transform.position;
		dPosition = transform.eulerAngles;

		 if (playerposition == 1){
			aPosition = Vector3(-418.2407, 60, 315.9193); //position
			bPosition = Vector3(0, 46.48776, 0); //rotation
		}
		else if (playerposition == 2){
			aPosition = Vector3(-257.0427, 60, 149.6062);
			bPosition = Vector3(0, 21.87396, 0);
		}
		else if (playerposition == 3){
			aPosition = Vector3(-3.386639, 60, -26.3466);
			bPosition = Vector3(0, 0, 0);
		}
		else if (playerposition == 4){
			aPosition = Vector3(340.5251, 60, 142.9057);
			bPosition = Vector3(0, 329.7681, 0);
		}
		else if (playerposition == 5){
			aPosition = Vector3(402.8006, 60, 334.6724);
			bPosition = Vector3(0, 311.4727, 0);
		}
	}

I just have a second but I might be able to help a little.

You do not need to use StartCoroutine() since the method you call does not have any yield instructions. You can call it directly since all you are doing is setting some values.

PlayerMoveSetup();

The line with the Quaternion has an error because you are using vectors for the first 2 arguements instead of Quaternions. The SmoothParentRotation script in the Physics example use the Quaternion.Slerp()

lastRotation = Quaternion.Slerp (lastRotation, transform.parent.rotation, smooth * Time.deltaTime);
transform.rotation = lastRotation;

I managed to get my script working. I had to abandon my original rotation values and by trial and error I found ones that work.

Could someone tell me why this line ends results in a camera rotation of around 30 degrees. I’m sure it’s some type of conversion but I;m not sure what or how.

bPosition = Quaternion.EulerAngles(0, .45, 0);

Secondly, I’d like these movements to be time dependant and not frame dependent. I have read that I should multiply by Time.deltaTime but this seems to produce erratic results. Maybe I’m multiplying the wrong value.

In any event, here is the script that seems to be working fine.

(That’s for the help lfrog.)

var playerposition = 3;
var aPosition : Vector3; //new position
var bPosition : Quaternion; //new rotation
var cPosition : Vector3; //current position
var dPosition : Quaternion; //current rotaion
var counting1 = 1000; 
var movespeed = .04;

aPosition = Vector3(-3.386639, 60, -26.3466);
bPosition = Quaternion.EulerAngles(0, 0, 0);

function Update () { 
   	if (Input. GetKeyDown ("a")){ 
		playerposition = playerposition - 1;
			if (playerposition == 0){
				playerposition = 5;
			}	
		PlayerMoveSetUp (); 
		}
	else if (Input. GetKeyDown ("b")){ 
		playerposition = playerposition + 1;
			if (playerposition == 6){
				playerposition = 1;
			}	
		PlayerMoveSetUp (); 
		}
	transform.position = Vector3.Lerp(cPosition, aPosition, (counting1* movespeed));   
	transform.rotation = Quaternion.Slerp(dPosition, bPosition, (counting1* movespeed));    
	counting1 =counting1 + 1;
	}

function PlayerMoveSetUp () {

		cPosition = transform.position;
		dPosition = transform.rotation;

		counting1 = 0;

		 if (playerposition == 1){
			aPosition = Vector3(-418.2407, 60, 315.9193); //position
			bPosition = Quaternion.EulerAngles(0, .80, 0); //rotation
		}
		else if (playerposition == 2){
			aPosition = Vector3(-257.0427, 60, 149.6062);
			bPosition = Quaternion.EulerAngles(0, .45, 0);
		}
		else if (playerposition == 3){
			aPosition = Vector3(-3.386639, 60, -26.3466);
			bPosition = Quaternion.EulerAngles(0, 0, 0);
		}
		else if (playerposition == 4){
			aPosition = Vector3(340.5251, 60, 142.9057);
			bPosition = Quaternion.EulerAngles(0, -.55, 0);
		}
		else if (playerposition == 5){
			aPosition = Vector3(402.8006, 60, 334.6724);
			bPosition = Quaternion.EulerAngles(0,-.80, 0);
		}
	}

[/code]

Quaternion.EulerAngles measures in radians.

Where as transform.eulerAngles measures in degrees.

That makes sense.

Thanks.