Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jun 26, 2016 at 06:16 AM by unicorn_lova.
avatar image
0
Question by unicorn_lova · Jun 26, 2016 at 06:04 AM · c#unity 5cameramovementrelative

how to make forward relative to the view of the camera

hello, I am currently making my first game in unity and i seam to be going good so far. I have made a movement system to roll the player(the player controls a ball) that works like this.

 void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }

I have the camera floating a bit behind the player using a camera holder inside the player. the reason I use a camera holder is so that when the player rolls the ball the camera will not rotate with the ball. The camera controller script just keeps the camera at the right angle and distance from the ball and looks like this.

 public GameObject CameraHolder;
 
     private Vector3 offset;
 
     void Start () {
         offset = CameraHolder.transform.position - transform.position;
     }

     void LateUpdate () {
         transform.position = CameraHolder.transform.position + offset;
         float desiredAngleX = CameraHolder.transform.eulerAngles.y;
         float desiredAngleY = CameraHolder.transform.eulerAngles.x;
         Quaternion rotation = Quaternion.Euler(desiredAngleY, desiredAngleX, 0);
         transform.position = CameraHolder.transform.position - (rotation * offset);
         transform.LookAt(CameraHolder.transform);
     }
 }
 

Everything was working fine up to this point until i wanted to make the camera rotate so you could look around. i use this script to make the camera holder rotate.

     public float RotateX = 0.0f;
     public float RotateY = 0.0f;
     public float RotateSpeed = 1.0f;
     public GameObject player;
     private Vector3 offset;
     // Use this for initialization
     void Start () {
         offset = transform.position - player.transform.position;
     }
     
     // Update is called once per frame
     void Update () {
     
         
         transform.Rotate (RotateY, RotateX, 0.0f);
         transform.position = player.transform.position + offset;
         RotateX = Input.GetAxis("Mouse X") * RotateSpeed;
         RotateY = Input.GetAxis("Mouse Y") * -RotateSpeed;
 
         transform.Rotate(RotateY, RotateX, 0.0f);
         transform.position = player.transform.position + offset;
         RotateX = Input.GetAxis("Joystick X") * RotateSpeed;
         RotateY = Input.GetAxis("Joystick Y") * RotateSpeed;
     }

now the camera will rotate around the ball like i want but the problem is with the movement. when i start the game and do not move the camera movement works like it should. up goes forwards down goes backwards ect. but when i rotate the camera the movement doesn't change. if i were to start the game and rotate the camera 180 degrees than up would go backwards what can i change to get the movement to be relative to the camera so that up will always go away from the camera and left will always go to the left of the camera ect.

sorry if i am not doing this post well this is my first tome posting.

Comment
Add comment · Show 1
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 unicorn_lova · Jun 26, 2016 at 06:15 AM 0
Share

sorry i figured it out after doing more research. just add movement = Camera.main.transform.TransformDirection(movement); after

 Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Related Questions

Navigation in VR Mode?!! 0 Answers

Stuttering nav Mesh Agent movement when i move the character 0 Answers

How to access camera Dont Clear in camera clear flags using c# 1 Answer

Need Help With Having Player look at an object while stationary. 0 Answers

help with player movement 0 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