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 raptorkwok · Jan 15, 2014 at 10:52 AM · slerpendless runner

Endless Runner game: Turn Corners

I use the following codes in Update() to move the character, Runner, a GameObject :

 rigidbody.AddForce(new Vector3 (speed, 0, 0), ForceMode.Acceleration);

I use the following codes in Update() to change the direction of the Runner GameObject :

 if (Input.GetKey(KeyCode.LeftArrow)) {
   Quaternion target = Quaternion.Euler(0, 90, 0);
   gameObject.transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
 }

The Runner can rotate 90degree, but its movement direction does not change. Also, maximum rotation degree is 90degree, why? Did I misunderstand the use of the function Quaternion.Slerp() ?

Also, the Runner still runs at original direction, how to make the Runner change direction ( following the rotation ) ?

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

Answer by JoaoOliveira · Jan 15, 2014 at 11:09 AM

The rotation only goes up to 90 degrees because that's what you are using as target. You need to add 90 degrees to the target every time you turn.

As for why the runner always runs in the same direction, that's because you are always applying the same force direction, (speed, 0, 0). You can change it to be always directed to the character's forward direction like this:

 rigidbody.AddForce(speed * transform.forward, ForceMode.Acceleration);
Comment
Add comment · Show 10 · 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 sanmn19 · Jan 15, 2014 at 11:11 AM 0
Share

Or use AddRelativeForce( new Vector3(speed,0,0), Force$$anonymous$$ode.Acceleration);

avatar image raptorkwok · Jan 16, 2014 at 02:07 AM 0
Share

Understand the concept now. However, how can I turn 90 degree once "immediately" when I press a key ( say $$anonymous$$eyCode.LeftArrow ) ? Should I replace Quaternion.Slerp() with other function ?

avatar image raptorkwok · Jan 16, 2014 at 02:08 AM 0
Share

Furthermore, what are the differences between AddForce(speed * transform.forward) and AddRelativeForce() ? Is there a performance difference ?

avatar image raptorkwok · Jan 16, 2014 at 08:17 AM 0
Share

Alternatively, I use the function Quaternion.AngleAxis() to rotate the target:

     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
         gameObject.transform.rotation = Quaternion.AngleAxis(90, Vector3.up);
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
         gameObject.transform.rotation = Quaternion.AngleAxis(-90, Vector3.up);
     }

But this will turn the target to exact angle, not modifying exist angle. What did I miss?

avatar image JoaoOliveira · Jan 16, 2014 at 09:41 AM 2
Share

Because this way you are setting the rotation to a certain value (either 90 or -90). What you want to do is to set it to currentRotation+90 or currentRotation-90.

You can do this by saving the current rotation value or by using transform.Rotate.

Show more comments

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

19 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

Related Questions

Moving player in an arc, from startPoint to endPoint 2 Answers

Having an action that occurs after a Quaternion slerp reaches goal 2 Answers

Follow and Rotate an object 1 Answer

Can you help me make this code turn smoothly? 0 Answers

How to Lerp or Slerp Time.timeScale change 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