Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smallville2010 · Sep 15, 2012 at 06:48 PM · camera movement

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }
         
     }
     
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Sep 15, 2012 at 07:11 PM 0
Share

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
 
 
avatar image
0

Answer by smallville2010 · Sep 16, 2012 at 01:37 AM

thank you soo much

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] Smooth Camera Movement Restrictions 0 Answers

How do I tell a button to move my camera!? 1 Answer

Mouse Look Help 2 Answers

Camera that both orbits and follows 1 Answer

How to rotate and move the camera with correct orientation,How to move camera after it gets rotated 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges