- Home /
Problem with lerping several objects to positions.
I have 4 game objects and 4 sockets. whenever i activate "row 1", i want each game object to jump to the adjacent socket and stay there until i activate "row 1" again. (object in socket 4 jumps to socket 1).
the socket and socketpos variables are linked to the same objects. It was necessary as Lerp asks for Transform, and Trigger asks for GameObject.
The following script is attached to each socket:
private var row; var Socket1 : GameObject; var Socket2 : GameObject; var Socket3 : GameObject; var Socket4 : GameObject; var Socket1pos : Transform; var Socket2pos : Transform; var Socket3pos : Transform; var Socket4pos : Transform
function OnTriggerStay (other : Collider) { if (row == 1) { if(other.gameObject == Socket1) { transform.position = Vector3.Lerp(transform.position, Socket2pos.position, Time.deltaTime *5); } if(other.gameObject == Socket2) { transform.position = Vector3.Lerp(transform.position, Socket3pos.position, Time.deltaTime *5); } if(other.gameObject == Socket3) { transform.position = Vector3.Lerp(transform.position, Socket4pos.position, Time.deltaTime *5); } if(other.gameObject == Socket4) { transform.position = Vector3.Lerp(transform.position, Socket1pos.position, Time.deltaTime *5); } } } The problem is; the objects just spin around uncontrollably, and i have no idea how to get them to just jump to the adjacent socket and stay. I have been stuck on this for days now, so i hope you pro's can help a noob scripter out! Thanks!
Use arrays ins$$anonymous$$d of multiple separate variables.
Answer by ScroodgeM · Jul 21, 2012 at 06:52 PM
transform.position = Vector3.Lerp(transform.position, Socket1pos.position, Time.deltaTime *5);
replace it with
Vector3 offsetNeeded = Socket1pos.position - transform.position;
float moveSpeed = 1f; // insert move speed here
if (offsetNeeded.sqrMagnitude > moveSpeed * moveSpeed * Time.deltaTime * Time.deltaTime)
{
offsetNeeded = offsetNeeded.normalized * moveSpeed * Time.deltaTime;
}
transform.position += offsetNeeded;
not tested
Answer by aldonaletto · Jul 22, 2012 at 05:46 PM
This logic seems wrong: supposing this script is added to the objects and the sockets are the triggers, each object will start moving to the next socket when row becomes 1, but will stop when the object leaves the current trigger (if the triggers don't overlap) or start a weird movement when entering the next socket trigger (if the triggers overlap).
I would use a different logic: in the object script, find all sockets at Start and populate an array, then control the object position by the array index - like this (object script):
var numSockets: int = 4; // how many sockets you have var curSocket: int = 0; // define each object's socket number in the Inspector var speed: float = 0.5; // speed to move to the next socket
private var sockets: GameObject[];
function Start(){ // allocate the sockets array: sockets = new GameObject[numSockets]; // populate it with objects named "Socket0", "Socket1", "Socket2" etc. for (var i=0; i< numSockets; i++){ sockets[i] = GameObject.Find("Socket"+i); } }
function Update(){ // find the actual curSocket position: var pos = sockets[curSocket].transform.position; // move towards it (does nothing if already at that position): transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime); }
function NextSocket(){ // function to move to the next socket: curSocket++; // increment socket number // if passed the last socket, cycle to the first one: if (curSocket >= numSockets) curSocket = 0; } When you want to make the objects jump to the next socket, call NextSocket in all objects. The easiest way to do that is to create an empty object (let's call it "Objects"), reset its position and rotation and child the four objects to it, then add the control script below, which will call the function NextSocket in each child via SendMessage:
function JumpToNextSocket(){
for (var child: Transform in transform){
child.SendMessage("NextSocket");
}
}
You must call JumpToNextSocket once in the control script when you want the objects to move to their respective next sockets
Your answer