Unity scripting newbie, but I have scripted in other environments.
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;
}
First advice, use C# instead.
Half kidding.
A) The code as written is a little difficult to read right now. My first recommendation would be: your trigger functions should ONLY set the target value, never the current value. Let Update shift currentValue and/or set the position. That’ll keep your movement smooth.
B) Use either Mathf.MoveTowards or Mathf.Lerp rather than your current mathy line. It’s more readable and straightforward.
C) You will be able to make your code simultaneously more versatile and more readable if you use Vector3’s and targetPosition in place of your targetValue. Vector3 has MoveTowards and Lerp functions as well (another reason to use those instead of your handwritten math). It’ll let you move these guys in any direction, configurable per object. Also add one private variable named initialPosition, which you set to the object’s position in Awake or Start.
D) transform.Translate is useful for moving an object by a specific “delta” amount, but what you want here is actually setting the position more absolutely. You should set transform.localPosition or transform.position instead of using .Translate. This is most likely the main source of your issue described here.
E) Combining A, C, and D, you could forego the usage of “currentValue” or an equivalent entirely, and lerp directly from whatever the current position happens to be. This will streamline your code and also virtually guarantee that no matter what happens to this thing, its movement will always smoothly head towards where it needs to go.
With all this in mind, your new script may look like this:
private var initialPosition : Vector3 = Vector3.zero;
public var movedPosition : Vector3 = new Vector3(0,1,0);
private var targetPosition : Vector3 = Vector3.zero;
public var easing : float = 0.02;
var ObjToMove : Transform;
function Awake() {
initialPosition = ObjToMove.localPosition;
//movedPosition = movedPosition + initialPosition; //optional; might be helpful depending on how you've set up these objects in your hierarchy
}
function Update(){
ObjToMove.localPosition = Vector3.Lerp(ObjToMove.localPosition, targetPosition, easing);
}
function OnTriggerEnter (other : Collider) {
targetPosition = movedPosition;
}
function OnTriggerExit (other : Collider) {
targetPosition = initialPosition;
}
Thanks for
“A) The code as written is a little difficult to read right now. My first recommendation would be: your trigger functions should ONLY set the target value, never the current value. Let Update shift currentValue and/or set the position. That’ll keep your movement smooth.”
Helped me out a bunch!