So I’m trying to get an object to move to a specific point on a click. I had this working in a separate scene with just a cube a very simple version of this code;
var smooth: int = 0;
private var targetPosition: Vector3;
function Update ()
{
targetPosition = Vector3 (3.3, -1.49, -4.34);
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
This works, however…
// checks for mouse over object
if ( Physics.Raycast ( ray, hit, 30 ))
{
// checks if we have clicked the button, and the object has a tag "resource"
if ( hit.collider.gameObject.tag == "resource" && Input.GetMouseButtonDown (0)) {
// gets the object _LevelMaster
var getResources = GameObject.Find("_LevelMaster");
var targetPosition = Vector3 (0, 0, 0);
// adds 25 to the resourceCount in _LevelMaster
getResources.GetComponent(LevelMaster).resourceCount += 25;
getResources.GetComponent(LevelMaster).UpdateHUD ();
hit.collider.gameObject.transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * resourceSpeed);
//Instantiate ( explosion, hit.collider.gameObject.transform.position, Quaternion.identity ); // plays explosion particle effect
//Destroy (hit.collider.gameObject); // deletes the object we have hit
}
}
This does not.
In the second code block, the object snaps instantly to the camera (which is the object housing this script) There is no smooth interpolation over time.deltaTime, and regardless of what coordinates I put in targetPostion, it always snaps to the camera.
Anyone have any ideas?