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 UndividedKira · Apr 01, 2020 at 11:49 AM · rigidbodybeginnerplayer movementrigidbody.velocity

How do i make local velocity?,How do i make velocity move the player the way the object is facing?

Hi I'm trying to make a player movement script using rigidbodies.I'm also pretty new to this so sorry if I have some mistakes or weird things with my code.I've decided to use velocity to move my player and I've set it up as well as camera rotation based on the mouse axis. (This also rotates the player)

But I noticed that even though my player rotates the way that I want him he doesn't really move the way he is facing. So bottom line is I was wondering how do I make him move locally based on the direction he is facing?

Here is my code:

 // Sets the rigidbody for the player

 public Rigidbody rb;

 // Sets a modifiable player speed

 public float speed = 0.0f;

 // Makes a empty vector for movement for forward,back,left,and right

 public Vector3 movement;

 // Checks if player is grounded

 public bool isGrounded;

 // calls the camera

 public GameObject icu;

 // Mouse sensitivity

 public float sensitivity = 0f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     // sets the movement and direction based on the axis of the keys

     movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

     if (Input.GetAxis("Mouse X") < 0)
     {

         transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse X") > 0)
     {

         transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") < 0)
     {

         icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") > 0)
     {

         icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);

     }

 }

 // Used for physics updates
 void FixedUpdate()
 {

     // Calls the module for player movement and sends our movement variable to said module

     playerMovement(movement);


 }

 // Makes a separate module for player movement and takes our module and sets it to help move the player in that direction

 void playerMovement(Vector3 direction)
 {

     rb.velocity = direction * speed;
 }

I was also curious if perhaps there was a good way to add a jump to the way I'm doing this?

,Hi, I'm new to most of this stuff but I'm using a rigidbody for my player and I've got it set up where the camera and object move based on the mouse axis. I also have it set up to where my player moves with velocity but it looks like it only moves globally. How can I change this to work local to the player's direction?

Here is my code:

 // Sets the rigidbody for the player

 public Rigidbody rb;

 // Sets a modifiable player speed

 public float speed = 0.0f;

 // Makes a empty vector for movement for forward,back,left,and right

 public Vector3 movement;

 // Checks if player is grounded

 public bool isGrounded;

 // calls the camera

 public GameObject icu;

 // Mouse sensitivity

 public float sensitivity = 0f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     // sets the movement and direction based on the axis of the keys

     movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

     if (Input.GetAxis("Mouse X") < 0)
     {

         transform.Rotate(Vector3.up * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse X") > 0)
     {

         transform.Rotate(Vector3.up * sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") < 0)
     {

         icu.transform.Rotate(Vector3.left * -sensitivity * Time.deltaTime);

     }

     if (Input.GetAxis("Mouse Y") > 0)
     {

         icu.transform.Rotate(Vector3.left * sensitivity * Time.deltaTime);

     }

 }

 // Used for physics updates
 void FixedUpdate()
 {

     // Calls the module for player movement and sends our movement variable to said module

     playerMovement(movement);


 }

 // Makes a seprate module for player movement and takes our module and sets it to help move the player in that direction

 void playerMovement(Vector3 direction)
 {

     rb.velocity = direction * speed;
 }
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 tormentoarmagedoom · Apr 01, 2020 at 02:15 PM

Hello.

There are 2 ways of calculate vectors in Unity, Local and World

If you calculate the movement vector using World coordinates, player will olways move same direction regardless its rotation.

 Vector3 direction = new Vector3(1,0,0);

But if you calculate it using Local coords, the vector ("ahead") is where the object is face (the blue arrow on the Editor)

 Vector3 direction = SomeObject.transform.forward

https://docs.unity3d.com/ScriptReference/Transform-forward.html

Bye!

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

179 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

Related Questions

Adding force relative to player's rotation with rigidbody.velocity 1 Answer

Can you pls help me fix this rigidbody collision problem 0 Answers

Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer

Is rigibody velocity frame rate independent? 1 Answer

Rigidbody physics - equalizing force 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