hi
I’m currently in the process of making a simple car racing game, for my main menu I’m using a 3D world menu and I’m wanting the camera to move across the world to a specific location once the user has pressed a menu button.
I’m guessing it’d be some form of onClick transform script I’m just not sure what
any suggestions?
I assume you’re talking about doing something like the title screen of “Mirror’s Edge” where there is a 3D view of the world in the background and the camera moves smoothly between preset positions when you click into a submenu.
First create a camera movement script that holds a list of transform objects representing the camera positions in world space.
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public Transform[] Transforms;
private int currentIndex;
void Awake()
{
SnapToPosition(0);
}
public void SnapToPosition(int index)
{
this.currentIndex = index;
this.transform.position = this.Transforms[index].position;
this.transform.rotation = this.Transforms[index].rotation;
}
public IEnumerator MoveToPosition(int index, float time)
{
if (this.currentIndex == index)
{
yield break;
}
Vector3 startPosition = this.transform.position;
Quaternion startRotation = this.transform.rotation;
Vector3 endPosition = this.Transforms[index].position;
Quaternion endRotation = this.Transforms[index].rotation;
this.currentIndex = index;
float elapsedTime = 0;
while (elapsedTime < time)
{
float a = elapsedTime / time;
this.transform.position = Vector3.Lerp(startPosition, endPosition, a);
this.transform.rotation = Quaternion.Slerp(startRotation, endRotation, a);
yield return new WaitForEndOfFrame();
elapsedTime += Time.deltaTime;
}
}
}
Then you can call the coroutine MoveToPosition to execute a smooth camera movement with something like this.
using UnityEngine;
using System.Collections;
public class MyGUI : MonoBehaviour
{
public CameraMover MyCameraMover;
void OnGUI()
{
if (GUILayout.Button("Move to 0"))
{
this.MyCameraMover.StartCoroutine(this.MyCameraMover.MoveToPosition(0, 1f));
}
if (GUILayout.Button("Move to 1"))
{
this.MyCameraMover.StartCoroutine(this.MyCameraMover.MoveToPosition(1, 1f));
}
if (GUILayout.Button("Move to 2"))
{
this.MyCameraMover.StartCoroutine(this.MyCameraMover.MoveToPosition(2, 1f));
}
}
}
Download this example project to see how to set up the objects. [21984-cameramove.zip|21984]
The key here is to have a list of transforms that represent your camera positions. The best way to position these transforms is just to create empty game objects and attach a camera component so you can preview the camera angle. But you MUST remember to disable these camera components before you release the game because they will all be rendering at the same time and slow down your frame rate. I suggest you modify the camera mover script to disable the cameras on all the GameObjects in the Transforms array so you don’t have to worry about this.
You can either attach a script to the camera that handles the movement whenever you press a button or if it makes sense to have it another place you just need to find the camera through your scripting, before you can transform it.
If you have the script attached to the camera, the code could look something like this:
if(Input.GetKey(KeyCode.D)) {
gameObject.transform.position = new Vector3(x, y, z);
}
If however, it is attached to another object and you need to find your camera, this can be achieved by this:
Camera.main.transform.position = new Vector3(1,2,3);
reference:
thanks a lot for you suggestions its helped a lot with my work!