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 personWithQuestion · Dec 23, 2011 at 10:50 PM · referenceslerp

Explanation of slerp example in reference

Hey, the Vector3 slerp example in the Script reference contains the following. I've commented what I understand from it and reading wikipedia on slerp.

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
   public Transform sunrise;
   public Transform sunset;
 
   void Update() {
       // Is this first line a typo? When I tried this I divided the sum of
       // both positions instead of doing what they do here and it seemed
       // to work like it was supposed to. E.g. the object arced y-axis. In
       // contrast, using the code here caused it to arc on the z-axis. Anyway
       // this should be the midpoint between both positions.
       Vector3 center = sunrise.position + sunset.position * 0.5F;

       // Offset the midpoint so we now have the center of our arc. However, it's
       // offset in the negative y direction, whereas you'd think it would be offset
       // in the positive y direction.
       center -= new Vector3(0, 1, 0);

       // Sunrise position relative to the center, so this will be in the positive
       // y direction.
       Vector3 riseRelCenter = sunrise.position - center;

       // Sunset position relative to the center, in +y direction.
       Vector3 setRelCenter = sunset.position - center;

       // Magic. Reference says 'interpolates from toward to by amount t'. The t
       // argument isn't confusing, I get that they are just taking advantage of
       // using the decimal values during the first second. But what are the first
       // two parameters supposed to be exactly? Are they supposed to be start and
       // end positions relative to our center? Again though, why is the center
       // located below where the actual center of the arc is (at least from what
       // I saw by running the example).
       transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, Time.time);

       // No idea.
       transform.position += center;
   }
 }

I tried the above just having a cube arc from one position from another, with the only change being the center being originally calculated as (sunrise.position + sunset.position) 0.5 rather than sunrise.position + (sunset.position 0.5) as above. The cube in the +y direction from one point to another. If I left the example was it was without changing the first statement it arced in the positive z direction.

Just looking for an explanation of what's going on. Particularly the reason for calculating center in that way, and adding center to the position at the end.

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 aldonaletto · Dec 23, 2011 at 11:25 PM

1- You're right about the center calculation: it's a typo in the C# example - the javascript version is ok.
2- The reason to move the center a little down is to force the movement to occur above the two points - slerp probably goes in the shortest direction, thus shifting the center a little below the line between the two points makes slerp "choose" the arc above them.
3- Apparently, slerp follows an arc centered at (0,0,0); since the calculated center may be anywhere, the smart Unity guys shifted the whole thing to (0,0,0) by subtracting the center coordinates from sunrise and sunset: they calculate slerp using these shifted from and to points, then add the center coordinates again to move the calculated point to where it should be - that's the purpose of the mysterious last line.
4- The 3rd parameter is a number between 0 and 1, where 0 returns from position, 1 returns to position, and any value between these values return a proportional position in the arc.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Launching local Unity API Reference from MonoDevelop 7 Answers

Using C# how can I setup a "database" script? 1 Answer

Error message "variable not assigned" ? 1 Answer

build for iphone - "function is not a member of object" 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