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
1
Question by ysleungrockman · Mar 21, 2018 at 08:12 AM · transform.forwardrigidbody.moveposition

Rigidbody MovePosition based on the direction facing, but with x and z values calculated separately?

Currently, I'm doing an HTC Vive project, and the character which player control is moved by touching on the touch pad. Here is the specific line of code that controls how the character moves currently:

 body.MovePosition (new Vector3 (body.position.x + vector.x * 3 * Time.deltaTime, body.position.y, body.position.z + vector.y * 3 * Time.deltaTime));

The vector.x and vector.y are the components of the coordinate which gets from the touch pad. They contain values ranged from -1 to 1, and they are used to control the direction and speed of the character's movement. However, I don't know how to add something like transform.forward in the formula to make the character's movement based on the direction it faced. So is it possible to add transform.forward or other direction to the formula like this? If yes, how can I change the formula?

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
1
Best Answer

Answer by Harinezumi · Mar 21, 2018 at 08:25 AM

You can either use Vector3.Scale() on your transform.forward (see example below), or you could try converting the touch vector into the world space direction defined by transform using Transform.TransformDirection(). Both approaches will require some tweaking (e.g. normalizing vectors).

 // example 1:
 Vector3 displacementDirection = transform.forward.Scale(new Vector3(vector.x, 0, vector.y)); // new vector needed because input coordinates are xy not xz
 
 // example 2:
 Vector3 displacementDIrection = transform.TransformDirection(new Vector3(vector.x, 0, vector.y));
Comment
Add comment · Show 3 · 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 ysleungrockman · Mar 21, 2018 at 10:38 AM 0
Share

So after calculating the displacementDirection, am I just need to change the code to this:

 body.$$anonymous$$ovePosition (new Vector3 (body.position.x + displacementDirection.x * 3 * Time.deltaTime, body.position.y, body.position.z + displacementDirection.z * 3 * Time.deltaTime));

And it is okay? And what do you mean by requiring some tweaking?

avatar image Harinezumi ysleungrockman · Mar 21, 2018 at 10:51 AM 0
Share

Yes, that should work, but I would just use body.$$anonymous$$ovePosition(body.position + displacementDirection * 3 * Time.deltaTime);. The y component should stay 0, but you can use Vector3.ProjectOnPlane(displacementDirection, Vector3.up) to make sure.
By tweaking I mean for example what I wrote above, that you might need to project the displacement vector to not have y component, possibly even normalize it. Basically take into consideration small issues that can occur.
Btw, I would not use the constant 3 in the move, ins$$anonymous$$d declare a float moveSpeed variable and use that. It will make the code more flexible and less prone to errors.

avatar image ysleungrockman Harinezumi · Mar 23, 2018 at 02:33 PM 1
Share

I see. It's working now! Thank you very much!

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

75 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

Related Questions

How to lock an object's transform.forward? 1 Answer

How Unity define transform.forward of humanoid avatar with root motion? 2 Answers

Creating Extending/Retracting Piston. Extends OK, won't retract 1 Answer

Cardboard Camera Movement 1 Answer

How to find the direction an object is flying in? 2 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