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 Mergster · Dec 06, 2017 at 12:45 AM · movementcharacterrotateridgidbody

Character Rotate Properly

With this script, I can move and rotate my character.

 Rigidbody rb;

 public float speed = 6.0F;
 public float forwardSpeed;
 public float turnSpeed;
 private Vector3 moveDirection = Vector3.zero;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody> ();

 }
 
 // Update is called once per frame
 void FixedUpdate () {

     rb.AddRelativeForce(0, 0, forwardSpeed * 100 * Time.deltaTime);
     
     moveDirection = new Vector3(Input.GetAxis("Horizontal"), -Input.GetAxis("Vertical"), 0);
     moveDirection = transform.TransformDirection(moveDirection);
     rb.AddRelativeForce(moveDirection * speed * Time.deltaTime, ForceMode.VelocityChange);

     transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (moveDirection), turnSpeed * Time.deltaTime);

     
 }


However, I can rotate around in a circle and my rotation really doesn't follow my movement. It's kinda hard to explain. I'm trying to accomplish a moving system as in Mario Odyssey's Bullet Bill (https://www.youtube.com/watch?v=wuGWBjB3-Mo&feature=youtu.be&t=479)

Thanks so much for any help :)

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

Answer by Kwergan · Dec 06, 2017 at 02:13 AM

It seems that you are using the moveDirection as input for the rotation. This will rotate the character towards the direction of the controller input rather than the velocity, which is what I think you want. Try:

transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (rb.velocity), turnSpeed * Time.deltaTime);

Comment
Add comment · Show 17 · 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 Mergster · Dec 06, 2017 at 02:28 AM 0
Share

Thanks for the answer! This is an improvement but my character starts to tremble when it rotates to a certain degree. $$anonymous$$aybe I should try a different approach altogether...:\

avatar image Kwergan Mergster · Dec 06, 2017 at 02:34 AM 0
Share
 transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (rb.velocity), Vector3.Angle(rb.velocity, transform.forward) * turnSpeed * Time.deltaTime);

The Vector3.Angle uses the angle between forward and the desired direction as a factor of rotation. So the character should rotate faster the further they are from the target. You may have to tweak turnSpeed. @$$anonymous$$ergster

avatar image Mergster Kwergan · Dec 06, 2017 at 03:02 AM 0
Share

No luck....It stopped trembling(except if you hold down the up key for a long time) but this movement doesn't seem dynamic no matter what value I use for turnSpeed.

Show more comments
avatar image
0

Answer by fdz_ · Dec 08, 2017 at 11:07 PM

is this any helpful? tutorial by N3K EN for Fly Control

Comment
Add comment · Show 2 · 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 Mergster · Dec 09, 2017 at 12:17 AM 0
Share

Not really...It would be if the tutorial was about how to make that work with a Rigidbody. I already know how to do that with a Character Controller and I rather wanted to do this with a Rigidbody for collision detection and other things... Thanks anyway though. :)

avatar image fdz_ Mergster · Dec 09, 2017 at 12:28 AM 0
Share

What if you use the script from the dude, but you change the last line:

 charController.$$anonymous$$ove(moveVector * Time.deltaTime)

to:

  rb.velocity += moveVector * Time.deltaTime

or:

 rb.AddForce( moveVector )

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

128 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

Related Questions

Rotate Character with movement 3 Answers

Two Keys ws or wd detection 3 Answers

How can I stop player snapping back to 0,0,0 rotation when I release keys? 1 Answer

I Watched The Unity New Ball Rolling Game Tut, How I Can Make Jump,I Watched The New Unity Ball Rolling Tutorial, How I Can Make Jump? 0 Answers

Character Animation Question (Bear Force One) 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