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 /
avatar image
0
Question by JordanW1195 · Mar 18, 2016 at 12:52 PM · c#cameramovement scriptrelative

How do I make the movement relative to the camera?

I am trying to create my own third person controller using C#. Currently the players movements seem to be relative to the world rather than the camera. I want the movement to be relative to the camera, for example pressing the 'W' key will make the player move away from the camera. Any advice or solutions?

Here is my current script:

public class PlayerMovement : MonoBehaviour { public float movementSpeed = 10f; public float turningSpeed = 60f;

 private CharacterController characterController;

 void Start (){
     this.characterController = GetComponent<CharacterController>();
 }

 // Update is called once per frame
 void Update () {
     float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
     transform.Rotate(0, horizontal, 0);

     this.characterController.transform.Rotate(0, horizontal, 0);
     
     float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
     Vector3 movement = new Vector3(0, 0, vertical);
     this.characterController.Move (movement);

 
 }

}

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
0

Answer by Liraseth · Oct 09, 2019 at 07:07 PM

Ok I got this to work well with the help of other posts. Make sure that at the start of the script there is...

 public float speed;
 private Rigidbody rb;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

then the main part...

 void FixedUpdate()
 {
     Vector3 movement;

     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");

     GameObject cameraCompass = new GameObject("Camera Compass");
     // "Correct" the compass so that it lies flat in the game plane
     cameraCompass.transform.eulerAngles = new Vector3(0, GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y, 0f);

     Vector3 movementX = cameraCompass.transform.right * moveHorizontal;
     Vector3 movementZ = cameraCompass.transform.forward * moveVertical;
     movement = movementX + movementZ;

     rb.AddForce(movement * speed);
     
     Destroy(cameraCompass); // We don't need the Compass anymore!
 }

Hopefully this helps!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to have a child object camera with gaze move its parent object 0 Answers

Third person movement with fixed cameras 1 Answer

how to make forward relative to the view of the camera 0 Answers

How to make an object go the direction it is facing? (Im new) 0 Answers

Face direction of a Vector 3 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