- Home /
How to move the camera to specific location using a script ?
hi ! i'm making a Quest game in unity and I want to write a script so that when i press an object (let's say an arrow...) the camera will move to a specific location, the thing is i want it to be precise so I can't do an animation to the camera then just play the animation because then the camera will be in a different position every time. how can i do it so that the camera will transform(or move...) into that EXACT position nicely ?
thanks allot !!! for answering...
SV
Answer by whydoidoit · Sep 15, 2012 at 07:06 PM
Make an empty game object at the location you want the camera to be - then when you press the arrow simply set Camera.main.transform.position to the position of the game object and Camera.main.transform.rotation to the rotation of the game object.
You could attach this script to the empty game object which gives you a way to visualise the position and rotation.
using System.Collections;
using UnityEngine;
public class PathPosition : MonoBehaviour {
public string description;
public void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, transform.position + transform.forward/2);
Gizmos.DrawSphere(transform.position + transform.forward/2, 0.02f);
if(!string.IsNullOrEmpty(description)) TextGizmo.Draw(transform.position, description);
}
}
You could also use this script which adds a "Camera" top level menu and sets up the empty game object for you at the scene cameras current position - or moves the scene camera to the position so you can see what it would look like. It needs to be used in conjunction with the PathPosition script above.
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
public class PlaceLookPosition : Editor
{
[$$anonymous$$enuItem("GameObject/Align y")]
public static void AlignY()
{
if(Selection.gameObjects.Length < 2)
return;
Undo.RegisterSceneUndo("Align y");
var pos = Selection.activeGameObject.transform.position;
foreach(var go in Selection.gameObjects)
{
var p = go.transform.position;
p.y = pos.y;
go.transform.position = p;
}
}
[$$anonymous$$enuItem("Camera/Place Look Position %p")]
public static void Place()
{
var camera = SceneView.lastActiveSceneView.camera;
if( camera == null)
return;
if(Selection.activeGameObject == null || Selection.activeGameObject.GetComponent<PathPosition>()==null)
{
var go = new GameObject();
go.name = "LookPosition";
go.AddComponent<PathPosition>();
if(Selection.activeGameObject)
{
go.transform.parent = Selection.activeGameObject.transform;
}
go.transform.position = Camera.current.transform.position;
go.transform.rotation = Camera.current.transform.rotation;
} else if(Selection.activeGameObject != null)
{
Selection.activeGameObject.transform.position = camera.transform.position;
Selection.activeGameObject.transform.rotation = camera.transform.rotation;
}
}
[$$anonymous$$enuItem("Camera/Goto Look Position _l")]
public static void Goto()
{
var camera = SceneView.lastActiveSceneView.camera;
if( camera == null)
return;
if(Selection.activeGameObject != null && Selection.activeGameObject.GetComponentInChildren<PathPosition>()!=null)
{
Selection.activeGameObject = Selection.activeGameObject.GetComponentInChildren<PathPosition>().gameObject;
SceneView.lastActiveSceneView.pivot = Selection.activeGameObject.transform.position;
SceneView.lastActiveSceneView.rotation = Selection.activeGameObject.transform.rotation;
SceneView.lastActiveSceneView.size = 1;
SceneView.RepaintAll();
}
}
}
#endif