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 coolbird22 · Sep 05, 2014 at 03:46 AM · arraypathslerpreplay

How to Slerp through values inside a 2D array ?

I have an 2D array which stores the transforms of a gameobject at every frame. My requirement is that when the gameobject reaches a checkpoint, the gameobject should go back to the first value and go through the same path again, but it doesn't have to be at a linear speed. Rather, it should start to speed up towards the end. I'm able to successfully make the gameobject go through the exact motion but unable to make it speed up towards the end. I basically wish to change the interpolation of how the array be able to speed up through its' values. The below is the snippet of how I'm making the gameobject go through the values stored inside the array at a linear speed.

     myTrans tp = new myTrans (); //create temp variable our class
             tp.myPos = this.transform.position; //save current position
             tp.myQuat = this.transform.rotation; //save current rotation
             tp.myScale = this.transform.localScale; //save current scale
             Movements.Add (tp); //add current data in our list
             MovementIndex++;
 
 //This part above saves the values inside the array
 
 MovementIndex++;
                 if (MovementIndex >= 0 && Movements.Count > 0) {
                     this.transform.position = Movements[MovementIndex].myPos;
                     this.transform.rotation = Movements[MovementIndex].myQuat;
                     this.transform.localScale = Movements[MovementIndex].myScale;
                 }
 
 //This part above goes through the values inside the array making the object to go through the same path as before.
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
0
Best Answer

Answer by zombience · Sep 05, 2014 at 05:11 AM

one way you could do it is to use an AnimationCurve, draw in the interpolation curve that you'd want in the window, and then evaluate the linear value and return an interpolated value.

something like this:

 public AnimationCurve curve; // assign this in the inspector window
 
 public float linearInput = 0;
 public float interpolatedOutput;
 
 void Update()
 {
     linearInput += .01f;
     interpolatedOutput = curve.Evaluate(linearInput);
 }


as you run this script, you'll see interpolatedOutput move according to the curve that you've drawn in the AnimationCurve window in the inspector. I find this a quick and easy way to do such operations, because they are flexible and quick to edit. you can then multiply the output of your curve to match your desired range of output.

hope that helps

Comment
Add comment · Show 4 · 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 coolbird22 · Sep 07, 2014 at 05:46 PM 0
Share

Hello @zombience, apologies for a late reply. I tried this workflow. This idea seems like a particularly interesting way to interpolate values. However I could not manage to get this to work with the results I was expecting. I was trying to reverse the motion and go through the values in the $$anonymous$$ovementIndex in reverse. I assigned a graph curve such that it speeds up towards the end, but the interpolated output seemed pretty much the same.

         interpolatedOutput = curve.Evaluate($$anonymous$$ovementIndex--);
         $$anonymous$$ovementIndex = $$anonymous$$athf.CeilToInt( interpolatedOutput);
 
             if ($$anonymous$$ovementIndex >= 0 && $$anonymous$$ovements.Count > 0) {
                 this.transform.position = $$anonymous$$ovements[$$anonymous$$ovementIndex].myPos;
                 this.transform.rotation = $$anonymous$$ovements[$$anonymous$$ovementIndex].myQuat;
                 this.transform.localScale = $$anonymous$$ovements[$$anonymous$$ovementIndex].myScale;
             }
 
avatar image zombience · Sep 09, 2014 at 10:31 PM 1
Share

sorry for the late reply

i just tested this script out, and the interpolation works. perhaps your curve is not steep enough to perceive a difference?

 using UnityEngine;
 using System.Collections;
 
 public class ArrayTest : $$anonymous$$onoBehaviour 
 {
 
     public AnimationCurve curve;
     public float distance = 20;
     public int points = 100;
 
     public int idx;
 
     [Range(0f, .2f)]
     public float speed = .01f;
 
     public float evaluation;
     protected float index = 0;
     protected Vector3 endPoint;
     protected Vector3[] path; 
 
 
 
     void Start ()
     {
     
         endPoint = transform.position + transform.up * distance;
         path = new Vector3[points];
         Vector3 inc = (endPoint - transform.position).normalized * (distance / points);
         for(int i = 0; i < points; i++)
         {
             if(i == 0)
                 path[i] = transform.position + inc;
             else
                 path[i] = path[i-1] + inc;
         }
 
     }
     
     void Update () 
     {
         index += speed;
         if(index >= 1f || index < $$anonymous$$athf.Abs(speed))
             speed *= -1f;
 
         evaluation = curve.Evaluate(index) * (points - 1);
         idx = $$anonymous$$athf.FloorToInt(curve.Evaluate(index) * (points - 1));
         if(idx < 0)
             idx = 0;
 
         transform.position = path[idx];
 
     }
         
 }
 
avatar image coolbird22 · Sep 10, 2014 at 05:16 AM 0
Share

Ah, that was a rookie mistake. It does work and my curve was not indeed not steep enough. Thanks a lot for taking the time to do this, @zombience. I very much appreciate this. :)

avatar image zombience · Sep 10, 2014 at 05:45 AM 1
Share

certainly! glad to help ^_^

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

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

Related Questions

curved path, Slerp issue? 1 Answer

Enemy offset, gallaga style game (space invaders) 0 Answers

Array doesn't increase or add element 1 Answer

Gameboard, next position on a path 2 Answers

Remove particular objects from array 0 Answers


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