I am trying to take an object and move it when my character gets close to it, but return it to its original position when I move away.
I re-purposed a script I found for making an object rotate when you got close (like a door). In my edited version, I am able to get the object to move, but it won't stop moving and won't return to its position when I move away from it.
Any advice? Using Javascript...
var ObjMoveValue : float = 1.0;
private var targetValue : float = 0.0;
private var currentValue : float = 0.0;
private var easing : float = 0.02;
var ObjToMove : GameObject;
function Update(){
currentValue = currentValue + (targetValue - currentValue) * easing;
ObjToMove.transform.Translate(0, currentValue * Time.deltaTime, 0);
}
function OnTriggerEnter (other : Collider) {
targetValue = ObjMoveValue;
currentValue = 0.0;
}
function OnTriggerExit (other : Collider) {
// currentValue = ObjMoveValue;
targetValue = 0.0;
}
↧