- Home /
How to change object direction when Camera Rotates
Hi Peeps
How would I go about changing the object (Box) direction after the camera rotates either left or right 90 degrees. I will be disabling player controls while the camera rotates, The person playing will be able to rotate the camera left/right as many times as they like.
The idea is to always move the box away from the camera view if the W key is pressed.
Below is the code I've been given by: Bunny83 (His Unity Username) to move the box. But as I am only just starting to learn to code, I have no idea what part of the code to change.
I would be extremely grateful for any help with this. Thanks
#pragma strict
private var rotator : Transform;
var speed = 2.5;
var rotating = false;
function RotateCube(refPoint : Vector3, rotationAxis : Vector3)
{
// audio.PlayOneShot(audio.clip);
if (audio) audio.Play();
var size = renderer.bounds.extents;
refPoint = Vector3.Scale(refPoint - Vector3.up,size);
rotator.localRotation = Quaternion.identity;
rotator.position = transform.position + refPoint;
transform.parent = rotator;
var angle : float = 0;
while(angle < 90.0)
{
angle += Time.deltaTime*90.0*speed;
rotator.rotation = Quaternion.AngleAxis(Mathf.Min(angle,90.0),rotationAxis);
yield;
}
transform.parent = null;
rotating = false;
}
function Start()
{
rotator = (new GameObject("Rotator")).transform;
}
function Update ()
{
if (!rotating)
{
if (Input.GetKeyDown(KeyCode.D))
{
rotating = true;
RotateCube(Vector3.right,-Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.A))
{
rotating = true;
RotateCube(-Vector3.right,Vector3.forward);
}
else if (Input.GetKeyDown(KeyCode.W))
{
rotating = true;
RotateCube(Vector3.forward,Vector3.right);
}
else if (Input.GetKeyDown(KeyCode.S))
{
rotating = true;
RotateCube(-Vector3.forward,-Vector3.right);
}
}
}
Your answer
Follow this Question
Related Questions
Rotating a GameObject based on another GameObject's y-axis 1 Answer
Rotating gameobject after mouselook 0 Answers
How to rotate a gameobject (Y Axis) via c#? 2 Answers
Rotate an Object 1 Answer