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 /
avatar image
0
Question by PixelPit · Mar 17, 2020 at 07:59 PM · player movement

Player movement question (Unity3D)

Hi there, so I have a simple Unity 3D Player Movement Script. It works well, I can go left, right, forward, back etc. I also have a first person script for looking around which also works well, but some problems arise when I turn around and try use the player movement script. Everything seems to go inversed (Example, forward brings you backwards, vice versa) I was wondering if there was a problem with my code or a simple fix. All the best!

 void FixedUpdate ()
     {
         if (rb.velocity.y == 0)
         isGrounded = true;
 
         if (rb.velocity.y > 0.1)
         isGrounded = false;
 
         if (Input.GetKey(KeyCode.W))
            rb.AddForce (Vector3.forward * MoveSpeed);
 
            if (Input.GetKey(KeyCode.S))
            rb.AddForce (Vector3.back * MoveSpeed);
 
            if (Input.GetKey(KeyCode.D)) 
            rb.AddForce (Vector3.right * MoveSpeed);
 
            if (Input.GetKey(KeyCode.A)) 
            rb.AddForce (Vector3.left * MoveSpeed);
 
            if (Input.GetKeyDown(KeyCode.Space)) 
            rb.AddForce (Vector3.up * JumpForce);
     }
 }
 
 
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
1
Best Answer

Answer by metalted · Mar 17, 2020 at 08:05 PM

This is because you are using the world coordinate system. You can think of it this way: You are sitting in a car and you are driving in the z direction. Your head is facing forward and everything is fine. When you look out of the left window, do you expect the car to go that way too? Nope, its still heading in the z direction. That is what your code is doing too.


What you NEED, is that the body move relative to the heads coordinate system. So if you look out of the left window, and hit the gas, the car should go sideways, how stupid that may sound :P You can achieve this by using transform.TransformDirection(). Simple example:

 Transform head;
 Rigidbody body;
 
 body.AddForce(head.TransformDirection(Vector3.forward * moveSpeed);


You should check out these pages: https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html

Hope this gets you moving the right way!

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 PixelPit · Mar 22, 2020 at 04:29 PM 0
Share

Hi, sorry but I'm still kind of confused on what to do with it. How would I apply it to my code to get up and running? If needed, here is my First person looking around script that I have applied to my player gameobject using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class CameraScript : $$anonymous$$onoBehaviour
 {
     public float SensitivityX = 15f;
     public float SensitivityY = 15f;
 
 
     public float $$anonymous$$inimumX = -60f;
     public float $$anonymous$$aximumX = 60f;
     public float $$anonymous$$inimumY = -360f;
     public float $$anonymous$$aximumY = 360f;
 
     float RotationX = 0f;
     float RotationY = 0f;
 
     public Camera Cam;
     // Start is called before the first frame update
     void Start()
     {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
     }
 
     // Update is called once per frame
     void Update()
     {
          RotationY += Input.GetAxis("$$anonymous$$ouse X") * SensitivityY;
          RotationX += Input.GetAxis("$$anonymous$$ouse Y") * SensitivityX;
 
          //basically means that it constraints the value between the $$anonymous$$ and max.
          //Note we just constrain X rotation, Y rotation can go all the way around and keep going, that's fine
          RotationX = $$anonymous$$athf.Clamp(RotationX, $$anonymous$$inimumX, $$anonymous$$aximumX);
 
          //the player we just want to rotate around the Y-axis, nothing around the X or Z
          transform.transform.localEulerAngles = new Vector3(0, RotationY, 0);
          //But the Camera needs to rotate around both the X and Y axes
          Cam.transform.localEulerAngles = new Vector3(-RotationX, RotationY, 0);
 
 
 
     }
 }
 
avatar image
0

Answer by BBIT-SOLUTIONS · Mar 22, 2020 at 04:36 PM

You could try using transform.forward, transform.up, etc instead of the Vector3 ones.

That should take the local directions instead of the global ones.

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

129 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

Related Questions

How do I disable player input action map and enable the UI action map? 4 Answers

Making Player Rotation Smoother 0 Answers

Translating Player Movement and Clipping Through Walls 2 Answers

Click to move player while avoiding jitter at edges 0 Answers

How can I make it so the player can't move past a certain point? 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