- Home /
3D menu camera rotation issue.
Hello Community,
Trying to create a 3D main menu here. The main camera have to rotate around a single point to bring into the FOV other 3D buttons. So click on "Settings" to rotate the camera to the Settings Menu. The main camera is a chid of the object CameraCenter (tagged with "CameraCenter") where the rotation should be applied to.
My actual code is failing to work the way it should. The Rotation stops after few degrees. Where is my mistake?
#pragma strict
// button variables
var Title = false;
var Play = false;
var Settings = false;
var Quit = false;
var Back = false;
var ButtonSmooth = 2.0;
var ButtonMove = 2.0;
private var mouseover : int;
private var defaultPos : Vector3;
private var newPos : Vector3;
// camera variables
var CameraRotatinonSmooth = 2.0;
var CameraRotationAngle = 180;
var CameraCenter : GameObject;
private var defaultRot : Vector3;
private var newRot : Vector3;
function Start()
{
CameraCenter = GameObject.FindWithTag("CameraCenter");
defaultPos = transform.position;
newPos = new Vector3 (defaultPos.x, defaultPos.y, defaultPos.z + ButtonMove);
defaultRot = transform.eulerAngles;
newRot = new Vector3 (defaultRot.x, defaultRot.y + CameraRotationAngle, defaultRot.z);
}
// buttons moving toward camera
function Update ()
{
if(mouseover == 1)
{
renderer.material.color = Color.blue;
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * ButtonSmooth);
}
if(mouseover == 0)
{
renderer.material.color = Color.white;
transform.position = Vector3.Lerp(transform.position, defaultPos, Time.deltaTime * ButtonSmooth);
}
}
function OnMouseEnter()
{
mouseover = 1;
}
function OnMouseExit()
{
mouseover = 0;
}
function OnMouseDown()
{
if(Title)
{
// DO SOMETHING NICE
}
if(Play)
{
Application.LoadLevel(1);
}
if(Settings)//camera rotation to settings part
{
CameraCenter.transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, newRot, Time.deltaTime * CameraRotatinonSmooth);
}
if(Back)//camera rotation back to main menu
{
CameraCenter.transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * CameraRotatinonSmooth);
}
else
{
Application.Quit();
}
}
Ps. already tryed to commit the rotation via CameraCenter.animation.Play. Works fine, but i would apritiate a script only solution to have more tuning possibilities.
Answer by Scribe · Jan 30, 2015 at 12:32 PM
Your problem is that OnMouseDown is only called once, and so your Slerp methods only get called once with a small t value and then stop and aren't called again.
You could handle it similarly to how you save the mouseover number and then do an action in update while that number is something or alternatively you could use coroutines to handle the action over some amount of time an example to follow (my UnityScript knowledge is a little rusty so they may be a few errors):
...
if(Settings)//camera rotation to settings part
{
StartCoroutine(CameraRotate(CameraCenter.transform, newRot, 1/CameraRotatinonSmooth));
}
...
function CameraRotate(trans : Transform, endRot : Vector3, time : float){
var startRot : Vector3 = trans.eulerAngles;
var t : float = 0;
while(t <= 1){
trans.eulerAngles = Vector3.Slerp(startRot, endRot, t);
t += Time.deltaTime/time;
yield null;
}
trans.eulerAngles = endRot;
}
this way it will continue until CameraRotate has finished even is OnMouseDown is not called again.
Scribe
Your answer
Follow this Question
Related Questions
NGUI- lerping widget on Y Axis only working in one direction 0 Answers
Camera Lerp from A to B, and back 2 Answers
Use Lerp Position and Slerp Rotation together 0 Answers
Quaternion Slerp Sanity Check 1 Answer