Rotating object attached to a rolling ball...

Hey guys,

I’ve got a rolling ball with an EmptyObject attached to it. On this EmptyObject I have a box which is attached to the EmptyObject by a spring. This all works well although I have now tried to associate a script with the box which when the ball moves, the face of the box turns to move in that direction as well.

Here is the script I am using for this:

#pragma strict

function Start () {

}

function Update () {

transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));

}

This turns the box perfectly well in the direction the ball is moving, but when you release a key, the box goes back to its default position (I guess this is its ‘z’ position). Is there a way to keep the direction that the box object is turned in and stay there until another key is pressed?

Thanks in advance.

R

Here is a bit of untested code.

EDIT: Changed my code, think this may work better in your case.

#pragma strict

var direction : Vector3 = Vector3.forward;
var rotateSpeedDegreesPerSec : float = 180.0f;

function Update () {

	direction = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
	
	if(direction != Vector3.zero)
	{
		transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(direction), rotateSpeedDegreesPerSec * Time.deltaTime);
	}

}