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
1
Question by Ziboo · Dec 20, 2011 at 11:25 PM · carwaypoints

Car Rotation

Hi guys,

I'm posting cause I looked everywhere to find a solution, tested everything and I didn't find a solution.

I'm making a simple car network, car going from waypoint to waypoint.

My car is just a rigidbody, with sphere colliders on the wheels, they are facing Z and Y is Up.

I let the physic(rigidbody) and gravity tacking care of the X and Z rotations.

Now I want to face the target waypoint, but it seams that I'm not having the good solution, cause sometime the car is going crazy.

I think I would like to end up with an angle to turn in Y, but whatever the rotation in X and Z, I guess this is the solution to after rotate my car from this angle.

Here is the code I have so far:

Thank You for your Help


     Vector3 dir = (new Vector3 (targetNode.transform.position.x, targetNode.transform.position.y, targetNode.transform.position.z) - new Vector3 (transform.position.x, transform.position.y, transform.position.z)).normalized;
     dir.y = 0;
     float direction = Vector3.Dot (dir, transform.right);
     var angle = Vector3.Angle (new Vector3 (transform.position.x, 0, transform.position.z), new Vector3 (targetNode.transform.position.x, 0, targetNode.transform.position.z));
     Vector3 MyRot = rigidbody.rotation.eulerAngles;
     
     if (direction > .1f) {            

         rigidbody.rotation = Quaternion.Slerp (rigidbody.rotation, Quaternion.Euler (MyRot.x, MyRot.y + angle, MyRot.z), Time.deltaTime * turnSpeed);
     }

     if (direction < -.1f) {
         
         rigidbody.rotation = Quaternion.Slerp (rigidbody.rotation, Quaternion.Euler (MyRot.x, MyRot.y - angle, MyRot.z), Time.deltaTime * turnSpeed);

     }
     
     //Add Gravity

     rigidbody.AddRelativeForce (-Vector3.up * 5000 * Time.deltaTime);        

     //Add speed and clamp speed

     if (rigidbody.velocity.magnitude < speed * .3f)

         rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime, ForceMode.VelocityChange);
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 akgdeen · Dec 28, 2011 at 03:08 PM

Below code may help you,

      var targetPoint:Vector3 = targetPoint.transform.position;
      var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
      transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3.0);    
      rigidbody.AddRelativeForce (0, 0, speed);
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 Ziboo · Dec 29, 2011 at 12:03 AM 0
Share

Thank you for you answer, but this is what I want to avoid. Cause the car is looking the target point. But I just want the car too point toward the target, meaning just changing the Y rotation, and not the X.

avatar image Ziboo · Dec 29, 2011 at 12:41 AM 0
Share

Found it:

Vector3 $$anonymous$$yRot = rigidbody.rotation.eulerAngles; var lookRotation = Quaternion.LookRotation (targetNode.transform.position - transform.position);

rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, Quaternion.Euler (new Vector3 ($$anonymous$$yRot.x, lookRotation.eulerAngles.y, $$anonymous$$yRot.z)), Time.deltaTime * turnSpeed);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Open world Waypoints NPC and Cars how? 0 Answers

Unity 5 Standard Asset Car Waypoint AI problem with accuracy 0 Answers

How To Add AI To Car/Racer? 4 Answers

How to make an AI of a car to jump if there is any obstacle 2 Answers

car colliding problem 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