Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 gm091203 · Mar 23, 2018 at 12:32 PM · camerascripting problemmovementdirectiongeneral

Move player according to the camera direction

Hi, I want to be able to make my player move forward in different directions depending on what direction the camera is facing, so say if by default my player moved north when I pressed W (W = forward), how could I make it that if my camera was facing east my player would follow in the east direction instead of the north? I will list my Camera and Movement script files below...

Camera file:

 [Header("Variables")]
 public Transform player;

 [Space]
 [Header("Position")]
 public float camPosX;
 public float camPosY;
 public float camPosZ;

 [Space]
 [Header("Rotation")]
 public float camRotationX;
 public float camRotationY;
 public float camRotationZ;

 [Space]
 [Range(0f, 10f)]
 public float turnSpeed;

 //Misc
 public static bool CamOrbit = true;

 private void Start()
 {
     offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
     transform.rotation = Quaternion.Euler(camRotationX, camRotationY, camRotationZ);
 }


 private void LateUpdate()
 {
     offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
     transform.position = player.position + offset;
     transform.LookAt(player.position);
 }

Movement file:

 //"rb" Is A Reference To Rigidbody
 public Rigidbody rb;

 //Internal Names To Change Values In Unity
 public float forwardForce = 100f;
 public float sidewayForce = 100f;
 public float backwardForce = 100f;

 //Controls For Player
 void FixedUpdate ()
 {
     if (Input.GetKey("w"))
     {
         rb.AddForce(0, 0, forwardForce * Time.deltaTime, ForceMode.VelocityChange);
     }

     if (Input.GetKey("a"))
     {
         rb.AddForce(-sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }

     if (Input.GetKey("s"))
     {
         rb.AddForce(0, 0, -backwardForce * Time.deltaTime, ForceMode.VelocityChange);
     }

     if (Input.GetKey("d"))
     {
         rb.AddForce(sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
     }
 }


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 TreyH · Mar 23, 2018 at 03:10 PM 0
Share

Since all of your Y components are 0, I'm assu$$anonymous$$g your movement is locked to the XZ plane?

1 Reply

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

Answer by Vyzier · Mar 23, 2018 at 02:06 PM

Try this: for each of your AddForce, make it so that you're passing a whole Vector3 parameter instead of separate floats. Then pass them the main camera's transform.forward/right multiplied by the amount of forces and Time.deltaTime. So, something like this:

 // I'm just making variables for these things so our lines of code don't get too long
 Vector3 forward = Camera.main.transform.forward;
 Vector3 right = Camera.main.transform.right;
 
 // The .normalized here is important
 Vector3 forwardDir = new Vector3(forward.x, 0, forward.z).normalized;
 Vector3 rightDir = new Vector3(right.x, 0, right.z).normalized;
 
 if (Input.GetKey(KeyCode.W))
 {
     rb.AddForce(forwardDir * forwardForce * Time.deltaTime,
                 ForceMode.VelocityChange);
 }
 
 if (Input.GetKey(KeyCode.A))
 {
      rb.AddForce(rightDir * -sidewayForce * Time.deltaTime, 
                  ForceMode.VelocityChange);
 }
 
 if (Input.GetKey(KeyCode.S))
 {
     rb.AddForce(forwardDir * -backwardForce * Time.deltaTime,
                 ForceMode.VelocityChange);
 }
 
 if (Input.GetKey(KeyCode.D))
 {
     rb.AddForce(rightDir * sidewayForce * Time.deltaTime, 
                 ForceMode.VelocityChange);
 }


Edit: Updated answer so the direction of movement doesn't include the y-axis.

Comment
Add comment · Show 7 · 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 gm091203 · Mar 23, 2018 at 03:29 PM 0
Share

That works so good! Thank you so much... The only problem is, is when moving forward it's really slow, I've tried to change the force but it still seems to be going a bit slow, any idea why?

avatar image Vyzier gm091203 · Mar 23, 2018 at 04:51 PM 0
Share

Hmm.. I guess you can try changing the Force$$anonymous$$ode. Have you tried changing the force values to some ridiculous number? I'm not on my PC at the moment, so I can't really check this out right now.

avatar image gm091203 Vyzier · Mar 23, 2018 at 05:01 PM 0
Share

Yeah, I changed the force values the only dilemma with that is it mess' with my gravity and physics and launches you when you leave the edge of a game object...

Show more comments

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

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

Related Questions

i want my plane to fly in camera direction where head is facing (i am newb)please suggest some changes and better method 0 Answers

Camera Orbit Changing Characters Direction, How? How To Stop It? 0 Answers

Relative Movement Problem 1 Answer

How to move the main camera in Google Cardboard? 0 Answers

Camera relative 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