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 YeOldeSnake 1 · Aug 28, 2010 at 02:59 PM · rotationtransformaislerp

Error with Rotating script

var waypoint:Transform[]; var speed : float =5; private var currentWaypoint : int; var loop:boolean=true; var rotationSpeed:float=5;

function FixedUpdate () { if (currentWaypoint<waypoint.length){ var target = waypoint[currentWaypoint].position; var moveDirection : Vector3 = target - transform.position;

          var velocity = rigidbody.velocity;

         if(moveDirection.magnitude&lt;1){
         currentWaypoint++;
        }

        else{
        velocity = moveDirection.normalized*speed;
        }
 }
 else{
 if(loop){
  currentWaypoint=0;
 }
     else{
     velocity = Vector3.zero;
     }

}

rigidbody.velocity = velocity; transform.LookAt(target); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed); }

this is an AI script and the

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

Is supposed to make it rotate smoothly , but its making it spaz left and right and generally doesnt have the effect i want , what am i doing wrong

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

Answer by YeOldeSnake 1 · Aug 29, 2010 at 09:17 AM

Basically i found the problem

transform.LookAt(target);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

i had both a LookAt and the Slerp with the other LookAt , so i removed the first line and its ok also it went backwards but i found the problem. it was (transform.position-target) while it should be (target-transform.position)

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
avatar image
0

Answer by tylo · Aug 28, 2010 at 04:46 PM

I've found a Slerp/Lerp is more complex than it originally seems.

First of all, your line transform.LookAt(target) will instantly snap your object's Z-axis as looking toward that object with no transition effect. That may explain the first spaz motion.

Now for the Slerp. This is how I do it with a Mathf.Lerp function, which should work similarly.

float t = 0.0F; float hitPoints = 0; float maxHitPoints = 100; float buildTime = 30.0f; //Seconds

void Update() { //Or FixedUpdate if you want if ( hitPoints < maxHitPoints ){ t += Time.deltaTime; hitPoints = Mathf.Lerp(0, maxHitPoints, t / buildTime); } }

The problem is, with your line:

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((transform.position-target).normalized), Time.deltaTime * rotationSpeed);

Every time the Quaternion.Slerp is called, transform.rotation is changing. The next time Quaternion.Slerp is called, it uses a different transform.rotation to calculate with.

You want to try and eliminate the variables that change during the calculation if you want a smooth, linear transition.

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 YeOldeSnake 1 · Aug 28, 2010 at 05:37 PM 0
Share

so , what should i do, do you have a specific piece of code that would make my gameobject rotate smoothly?

avatar image tylo · Aug 28, 2010 at 05:53 PM 0
Share

I'm torn by giving you a specific set of code you can copy & paste. You should be able to solve the problem given this amount of information.

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

No one has followed this question yet.

Related Questions

Follow and Rotate an object 1 Answer

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

object pauses at the each way points for a vary short time 1 Answer

Smooth rotation problem 1 Answer

Quaternion Slerp Sanity Check 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