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 Saybejay · Mar 10, 2013 at 03:10 AM · cameratransformpositionrelativefacing

How do you move the camera relative to the direction it is currently facing?

Sorry if the question doesn't make much sense, but I hope I can explain my dilemma a bit better. First off, I'm not very experienced with Unity, or coding for that matter so for all I know the answer could be super simple.

Alright, so I have my code set up so I can move the camera along the X and Z axis by holding down the right mouse button and dragging. I also have the ability to rotate the camera on the Y axis by holding down the scroll wheel and dragging.

The problem I am having, is that when I rotate the camera something like 90 degrees, and I right click to move the camera it still moves in the same way (if that makes sense). So dragging the mouse horizontally moves the camera forward and backward along the X, while dragging vertically moves it left and right along the Z. So the camera always moves the same way, and not relative to which way it is facing.

Here's the code I have been using:

 var xPosition : float;
 var zPosition : float;
 var yRotation : float;
 var currentYRotation : float;
 var currentXPosition : float;
 var currentZPosition : float;
 var yRotationV : float;
 var xPositionV : float;
 var zPositionV : float;
 var moveSensitivity : float = 3;
 var lookSmoothDamp : float = 0.1;
 
 function Update ()
 {
     if(Input.GetMouseButton(1))
     {
         zPosition -= Input.GetAxis("Mouse Y") * moveSensitivity;
         xPosition -= Input.GetAxis("Mouse X") * moveSensitivity;
     
         currentXPosition = Mathf.SmoothDamp(currentXPosition, xPosition,       xPositionV, lookSmoothDamp);
         currentZPosition = Mathf.SmoothDamp(currentZPosition, zPosition, zPositionV, lookSmoothDamp);
     
         transform.position = Vector3(xPosition, 0, zPosition);
     }
     
     if(Input.GetMouseButton(2))
     {
         yRotation -= Input.GetAxis("Mouse X") * moveSensitivity;
         
         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation,   yRotationV, lookSmoothDamp);
         
         transform.rotation = Quaternion.Euler(0, yRotation, 0);
     }
 }


I actually have the camera attached to an empty game object which has the actual script attached to it, so I didn't know if it was possible to move the camera based on the position of the empty game object instead of world position. Though I don't even know if that would solve the problem.

Thanks in advance!

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

1 Reply

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

Answer by nsxdavid · Mar 10, 2013 at 03:39 AM

All Transform components have forward, right, and up vector that is relative.

The forward property is a Vector3 that points in the local Z direction. Up points in local Y, and right in local X. Obviously to get down you use -up.

So, to move something "forward" (assuming "forward" means "into the Z direction"), you can do:

 transform.position += transform.forward * distance;

If that's forward, then to move to the right:

 transform.position += transform.right * distance;

Left is:

 transform.position += -transform.right * distance;





Comment
Add comment · Show 3 · 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 Statement · Mar 10, 2013 at 04:16 AM 0
Share

And for arbitrary directions, you could use Transform.TransformDirection.

avatar image Saybejay · Mar 11, 2013 at 01:40 AM 0
Share

Thank you, thank you, thank you. I changed my transform.position = Vector3(xPosition, 0, zPosition) line to two if statements using the transform.forward and transform.right, and now it works wonderfully.

avatar image AlucardJay · Mar 11, 2013 at 01:55 AM 0
Share

Don't forget to mark the answer as accepted, by clicking on the grey tick, thanks. http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

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

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

Related Questions

Change position of camera on scene load? 1 Answer

How to get real local position of objects from many levels? 1 Answer

When switching camera position... 1 Answer

Help! I want to make a following camera but in with Y position is constant ? 2 Answers

Detect whenever the player is near the edges of the screen 2 Answers


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