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 SonicFan · Aug 12, 2014 at 02:50 PM · rotationsmoothtiltslope

Smooth tilting rotation on slopes like cars?

Hello,i know that there is many other questions like this one but non of them is what i want.

I'm making a game that i wan't when my player starts running to smoothly tilt on the slope and searched Google but i didn't find what i want but after so much searching yesterday I found this code.

    var adjustSpeed : float = 1;
    public var fromRotation : Quaternion;
    private var toRotation : Quaternion;
    private var targetNormal : Vector3;
    private var hit : RaycastHit;
    public var weight : float = 9;
      
     function Start() {
         targetNormal = transform.up;
     }
      
     function Update() {
     
         Debug.DrawRay(rotater.transform.position,-rotater.transform.up* 2, Color.yellow);
     
         if(Physics.Raycast(rotater.transform.position, -rotater.transform.up, hit)) {
             
            
            fromRotation = transform.rotation;
                 toRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
                 weight = 0;
                 
                 
             if(weight <= 1) {
                 weight += Time.deltaTime * adjustSpeed;
                 transform.rotation = Quaternion.Slerp(fromRotation, toRotation, weight);
                 //Or to smooth the weight
                 //tranform.rotation = Quaternion.Slerp(fromRotation, toRotation,
                 //                                        Mathf.SmoothStep(weight));
             }
         }
     }


This code is working Fine but it's rotating all of the axis but i don't want this code to rotate the y axis or my local y axis.

So i would like to know if there is a better code than this or i can modify this code to work better.

Thanks.

Comment
Add comment · Show 1
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 · Aug 12, 2014 at 11:03 PM 0
Share

I'm unclear about your desired behavior or what is going on, but your code is non-standard for aligning an object with the slope. Typically line 20 would be:

  toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SonicFan · Aug 13, 2014 at 06:42 AM

Sorry,i copied the wrong script because i was doing some tests on the script.

But i finally Fixed it and here is the script.

 var adjustSpeed : float = 1;
 public var fromRotation : Quaternion;
 private var toRotation : Quaternion;
 private var targetNormal : Vector3;
 private var hit : RaycastHit;
 public var weight : float = 9;
  
 function Start() {
     targetNormal = transform.up;
 }
 function Update() {
     if(Physics.Raycast(transform.position, -Vector3.up, hit)) {
       
             
             targetNormal = hit.normal;
             fromRotation = transform.rotation;
             toRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
             weight = 0;
             
         
         if(weight <= 1) {
             weight += Time.deltaTime * adjustSpeed;
             transform.rotation = Quaternion.Slerp(fromRotation, toRotation, weight);
             
         }
     }
 }
   
 

Thanks to robertbu for your comment because that was the problem.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Bank Camera Based On Rotation Delta 1 Answer

Rotate smoothly an object when key is up 0 Answers

Smooth reset rotation problem 1 Answer

EulerAngles conversion Quaternion problem 2 Answers

Choppy rotation of character around y-axis 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