Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dustxx · Feb 23, 2014 at 05:14 AM · movementrigidbodycontroller

How do you move a rigidbody by a vector3 relative to itself?

When I try and move a rigidbody with the MovePosition(Vector3) function, it moves in the direction of that actual vector3. I want it to move by that vector3 in the direction the rigidbody is already facing. here's my current code. What would I use to do this correctly?

 void FixedUpdate ()
 {
     //Take input for movement
     Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
     movement *= speed;
     //Move
     rigidbody.MovePosition (rigidbody.position + movement * Time.deltaTime);
 
     //Take input for rotation
     Vector3 rotation = new Vector3( 0f, Input.GetAxis("Turn"), 0f);
     rotation *= rotateSpeed;
     //Rotate
     rigidbody.MoveRotation (transform.rotation * Quaternion.Euler(rotation));
 }
Comment
Add comment · Show 4
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 robertbu · Feb 23, 2014 at 05:32 AM 0
Share

Your question as stated does not make much sense to me. You cannot both move in the direction the rigidbody is facing and also move 'by the vector3'. That is a vector3 is a direction. You could rewrite the character so that it always moves to its forward and it uses Input.GetAxis("Vertical") to calculate forward movement. Or?

avatar image robertbu · Feb 23, 2014 at 06:17 AM 0
Share

So is this code the answer to your question (it looks like it to me). If so, promote it from a comment to an answer and mark your question answered by clicking on the checkmark on the right side of your answer.

avatar image whydoidoit · Feb 23, 2014 at 06:18 AM 0
Share

And that works right?

avatar image dustxx · Feb 24, 2014 at 02:09 AM 0
Share

yes, I moved it to be an answer below.

1 Reply

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

Answer by dustxx · Feb 23, 2014 at 05:35 AM

ah, yeah. I realized that a bit after posting this question and I rewrote it to move based on the forward. Like this:

 //Take input for rotation
 Vector3 rotation = new Vector3( 0f, Input.GetAxis("Turn"), 0f);
 rotation *= rotateSpeed;
 //Rotate
 rigidbody.MoveRotation (transform.rotation * Quaternion.Euler(rotation));
     
 //Take input for vertical movement
 float moveVert = Input.GetAxis ("Vertical");
 moveVert *= speed;
 //Move vertically
 rigidbody.MovePosition (rigidbody.position + (transform.forward * moveVert) * Time.deltaTime);
     
 //Take input for horizontal movement
 float moveHorz = Input.GetAxis ("Horizontal");
 moveHorz *= speed;
 //Move horizontally
 rigidbody.MovePosition (rigidbody.position + (transform.right * moveHorz) * Time.deltaTime);

Sorry if the orighinal question was confusing

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Character controller jittering up and down 2 Answers

A simple solution for constant rigidbody movement without changing and clamping velocity directly 2 Answers

Move the rigidbody with keys 1 Answer

How to code PlayerController grid movement with RigidBody and Colliders? 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