- Home /
Slerps and / or other pendulum-like movements
Hey all,
Recently been working on adjusting my spider object in my game, and I've managed to come up with something (with the help of the kind folks at Unity Answers) that produces the desired result. Imagewise, I have a spider that is sitting on a branch (we'll call point A) who swings in an arc towards another branch (point B). When he reaches point B, he makes the return trip back towards point A. The arc is reminiscent of a pendulum.
The problem is that I've been using Slerp and while I'm happy with how the spider moves - I need away to adjust the depth of the arc. Here is my current code.
using UnityEngine;
using System.Collections;
public class swingSpider : MonoBehaviour {
public Transform start;
public Transform end;
public bool startToFinish;
public bool finishToStart;
public float slerpStartTime;
public float speed;
public float distance;
GameObject spider;
//public GameObject A;
//public GameObject B;
Vector3 spiderPos;
Vector3 startPos;
Vector3 endPos;
// Use this for initialization
void Start ()
{
startPos = start.transform.position;
Debug.Log(startPos);
endPos = end.transform.position;
Debug.Log(endPos);
startToFinish = true;
Debug.Log("sTF = true");
finishToStart = false;
Debug.Log("fTS = false");
speed = 0.1f;
slerpStartTime = Time.time;
transform.position = start.position;
Debug.Log("startPos = "+startPos);
//transform.position = end.position;
}
// Update is called once per frame
void Update ()
{
if(startToFinish == true)
{
Vector3 center = (start.position + end.position) * 0.5f;
center -= new Vector3(0,-0.1f,0);
Vector3 startRelCenter = start.position - center;
Vector3 endRelCenter = end.position - center;
distance = Vector3.Distance(transform.position, end.transform.position);
if(distance >= 0.1)
{
transform.position = Vector3.Slerp(startRelCenter, endRelCenter, (Time.time - slerpStartTime) * speed);
transform.position += center;
}
if(distance <= 0.1)
{
ResetA();
}
}
if (finishToStart == true)
{
Vector3 center = (start.position + end.position) * 0.5f;
center -= new Vector3(0,-0.1f,0);
Vector3 startRelCenter = end.position - center;
Vector3 endRelCenter = start.position - center;
distance = Vector3.Distance(transform.position, start.transform.position);
if(distance > 0.1)
{
transform.position = Vector3.Slerp(startRelCenter, endRelCenter, (Time.time - slerpStartTime) * speed);
transform.position += center;
}
if(distance <= 0.1)
{
ResetB();
}
}
}
void ResetA()
{
if(startToFinish == true)
{
startToFinish = false;
slerpStartTime = Time.time;
transform.position = end.position;
distance = 0;
finishToStart = true;
}
}
void ResetB()
{
if(finishToStart == true)
{
finishToStart = false;
slerpStartTime = Time.time;
transform.position = start.position;
distance = 0;
startToFinish = true;
}
}
}
I've found that I can adjust the y-coordinate of the Vector3 center to some degree, but not enough to fit my needs. I don't need the arc to be completely spherical, it can cylindrical as well.
Any suggestions how I can use the slerp function to produce the desired results? Or any other suggestions on how I can go about doing this?
I know Eric5h5 suggested looking into co-routines, but I wasn't able to find (let alone, write) anything useful.
Thanks again for your time!