Just can’t figure this out. I have an object with the following script attached that can be dragged along the Y axis with a mouse/finger, and when it collides with a trigger, it should lerp to another position. I can get the lerp to work without the dragging script, and the dragging script to work without the lerp, but once I put them together, no lerping happens and the object just snaps to 0,0,0. What about my code is interfering with the lerp?
// DRAGGING VARIABLES
var moveLimit = .5;
var collisionMoveFactor = .01;
var freezeRotationOnDrag = true;
var cam : Camera;
private var myRigidbody : Rigidbody;
private var myTransform : Transform;
private var canMove = false;
private var yPos : float;
private var freezeRotationSetting : boolean;
private var sqrMoveLimit : float;
private var camTransform : Transform;
// LERPING VARIABLES
var isLerping = false;
var lerpTimer = 0.0;
var lerpSpeed = 0.1;
var Begin_Rotation = transform.eulerAngles;
var End_Rotation = Vector3(-90,0,0);
var Begin_Position = transform.position;
var End_Position = Vector3(-4.229075,3,2.676687);
function Start () {
myRigidbody = rigidbody;
myTransform = transform;
if (!cam) {
cam = Camera.main;
}
camTransform = cam.transform;
sqrMoveLimit = moveLimit * moveLimit;
}
function OnMouseDown () {
canMove = true;
freezeRotationSetting = myRigidbody.freezeRotation;
myRigidbody.freezeRotation = freezeRotationOnDrag;
yPos = myTransform.position.y;
}
function OnMouseUp () {
canMove = false;
}
function OnTriggerEnter (other : Collider)
{
if (tag == other.gameObject.tag){
isLerping = true;
}
}
function FixedUpdate () {
if (!canMove) return;
myRigidbody.velocity = Vector3.zero;
myRigidbody.angularVelocity = Vector3.zero;
myTransform.position.y = yPos;
var mousePos = Input.mousePosition;
var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
myRigidbody.MovePosition(myRigidbody.position + move);
// LERPING CODE
if (isLerping == true){
lerpTimer += Time.deltaTime;
transform.eulerAngles = Vector3.Lerp (Begin_Rotation, End_Rotation, lerpTimer*lerpSpeed);
transform.position = Vector3.Lerp(Begin_Position, End_Position, lerpTimer*lerpSpeed);
}
}
Thanks, Sundar. I added your changes, but it still behaves the same... the shape still ends up at 0,0,0 with its default rotation. So confused by this.
– RussellFincherMade some changes to the above code, test and see whether this solves your lerp / move issue
– SundarThanks again. I get an error on line 16 saying the best overload for the method 'UnityEngine.Quaternion, UnitEngine.Quaternion, float) is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)'. Also the variable "move" is declared and used twice, but I made adjustments for that. But even when I comment out lines 16 and 17 (to ignore the rotation), now nothing happens when the shape is dragged across the trigger.
– RussellFincherLets do step by step, Check whether this works.
– SundarNo errors, the shape drags fine, but nothing happens when the shape enters the trigger.
– RussellFincher