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 pierscoe1 · Dec 16, 2013 at 03:42 PM · followsmoothslerpsurface

follow terrain script works, but Slerp breaks it!

I have this script making my car follow the undulations of the track:

 forward = Vector3.Cross(transform.right ,groundHit.normal ); 
 transform.rotation = Quaternion.LookRotation(forward, groundHit.normal); 

...which works great, but the alignment of the car to the track is obviously very snappy... so I tried this:

 forward = Vector3.Cross(transform.right ,groundHit.normal ); 
 targetRotation = Quaternion.LookRotation(forward, groundHit.normal); 
 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 20);

which does indeed smooth it out, but it seems to be breaking the rotation it's trying to get to... the car keeps nose-diving into the floor every time the surface normal changes...

any ideas?

Comment
Add comment · Show 7
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 · Dec 16, 2013 at 03:54 PM 0
Share

I assume the car is moving forward at the same time? What are you using for movement: character controller, rigidbody, directly changing the transform?

$$anonymous$$y visualization is that you are moving the car on it 'forward'. Since the rotation completes over time, the forward will be pointed into the surface during the time the car is realigning itself to the new surface.

avatar image pierscoe1 · Dec 16, 2013 at 05:05 PM 0
Share

using rigidbody AddRelativeForce to move the car forward...

what you've said makes sense actually... when you say the rotation completes over time, shouldn't it still be somewhere between transform.rotation & targetRotation, and hence not be pointing into the floor at any point!?

thanks for the input.

avatar image robertbu · Dec 16, 2013 at 05:28 PM 0
Share

alt text

This is what I'm thinking. The red arrow is what you would get with an immediate change. The green arrow is the Slerp() in progress. $$anonymous$$otion is into the hill. I don't know enough about your code, but I'm guessing that you are applying force in the objects forward direction. Perhaps the force should be applied along the red arrow even as the object is facing the green arrow.

intohill.png (8.9 kB)
avatar image pierscoe1 · Dec 16, 2013 at 08:54 PM 0
Share

Good shout. I'll give that a try tomorrow...

avatar image pierscoe1 · Dec 17, 2013 at 08:55 AM 0
Share

doh. still does the same :-( It seems a little better, but still nose-dives after moving foward a few meters...

here's the whole script, in case it helps:

 function FixedUpdate () {
 
     
     var x:Number = Input.GetAxis("Horizontal");
     var y:Number = Input.GetAxis("Vertical");
     var down = transform.TransformDirection (Vector3.down);
 
     
     currentSpeed = rigidbody.velocity.magnitude;
     
     var groundHit : RaycastHit;
     if (Physics.Raycast (transform.position, down, groundHit, 2)) {
     
 
 
         forward = Vector3.Cross(transform.right ,groundHit.normal ); 
         transform.rotation = Quaternion.LookRotation(forward, groundHit.normal); 
         
     //Smooth the ground-following!----------------
     //can't get this to work
         //targetRotation = Quaternion.LookRotation(forward, groundHit.normal);
         //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10);
     //Arghhh-------------------------------------------
     
     
         rigidbody.drag = 0.5;
         
         //Throttle
         //rigidbody.AddRelativeForce (Vector3.forward * y * forwardSpeed);
         rigidbody.AddForce (forward * y * forwardSpeed);
         
 
     
         currentVel = rigidbody.velocity.normalized;    
         slipAngle = Vector3.Angle(transform.forward, currentVel);
         
         //SlipDriving
         if(slipAngle > 40){
             rigidbody.AddForce(-rigidbody.velocity.normalized * currentSpeed * slipAngle * 0.01);
         }
     
         
         //Grip-Driving
         dirNum = AngleDir(transform.forward, currentVel, transform.up);
         if(slipAngle > 5){
             gripAssist = currentSpeed * (1/slipAngle) * dirNum * 8;
             rigidbody.AddRelativeForce(Vector3.left * gripAssist);
         }
         else{
             gripAssist = 0;
         }
         
         //Steer
         if(currentSpeed > 5){
             rigidbody.AddRelativeTorque (Vector3.up * x * turnSpeed * currentSpeed);
         }
         else{
             rigidbody.AddRelativeTorque (Vector3.up * x * turnSpeed * 20);
         }    
     }
     
     else{
         // Flight $$anonymous$$ode
         //Point-in-direction-of-flight
         //transform.forward = currentVel;
         var flightDir = Quaternion.LookRotation(currentVel, Vector3.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, flightDir, Time.deltaTime * 0.4);
 
         NoSmoke();
         rigidbody.drag = 0;    
     }
     
     
     //Road Gravity------------------
     var hit : RaycastHit;
     if (Physics.Raycast (transform.position, down, hit, 30.0)) {
         if(hit.distance < 20 && hit.distance > 0.5){
             rigidbody.AddRelativeForce (Vector3.down * 20);
         }
     }
     else{
         rigidbody.AddForce (Vector3.down * 5);
     }
     //------------------------------
     
     
 }
 
 
 
 function AngleDir(fwd: Vector3, targetDir: Vector3, up: Vector3) {
     var perp: Vector3 = Vector3.Cross(fwd, targetDir);
     var dir: float = Vector3.Dot(perp, up);
     
     if (dir > 0.0) {
         return 1.0;
     } else if (dir < 0.0) {
         return -1.0;
     } else {
         return 0.0;
     }
 }
 
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

smooth follow only on X and Y 1 Answer

SmoothFollow Unity Script improvements? 1 Answer

Unity 2D Object Smooth Follow Problem C# 2 Answers

Smooth Follow Rotation 3 Answers

How to Make the SmoothFollow Script Follow Faster? 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