- Home /
Move an object to a specific point?
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?
Well reading up on that page breakdown on Lerping,and it's interesting with regards to the various methods of going for a linear Lerp to one that eases in as it reaches it's destination...
But I'm still confused as to why my object ISN'T lerping in the second code block? All the proper parameters are there to tell it to move to a specific spot, but why on earth is it ignoring the code and snapping to the camera, or GameObject?
While I'm still struggling with this, I've discovered the problem as to why the object "snapped" to the camera.
In the Lerp statement, I specified the starting position as transform.position. This was wrong. transform.position refers to the GameObject that this script is attached too, which would be the $$anonymous$$ain Camera. And the reason why it wasn't moving is because this whole if statement only happens on click down. So on click, the resource would snap to the camera starting position, and because the click stopped, would not move from that position further.
the correct starting position should be the resource position or "hit.collider.gameObject.transform.position".
Now to figure out how to get the Lerp to complete it's movement outside of the If statement.
Answer by ArkaneX · Sep 17, 2013 at 03:24 PM
I see you answered half of the question, related to snapping. As to the second part, I suggest calling a coroutine and executing Vector3.Lerp inside, until it reaches the target. So, instead of this line
hit.collider.gameObject.transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * resourceSpeed);
which probably looks a bit different (after your fix), use this
MoveResource(hit.collider.gameObject.transform, targetPosition, resourceSpeed);
and add a coroutine code:
function MoveResource(resourceTransform:Transform, endPosition:Vector3, speed:float)
{
var startPosition = resourceTransform.position;
var t = 0.0;
while (t < 1.0)
{
t += Time.deltaTime * speed;
resourceTransform.position = Vector3.Lerp(startPosition, endPosition, t);
yield;
}
}
Thank you for taking the time to write up an example, ArkaneX! I more or less ended up doing something very similar to what you have here.
Thanks again for helping me out though, very appreciated!
Your answer
Follow this Question
Related Questions
How to translate object one movement with script 1 Answer
Player Transformation 2 Answers
Transform.Translate change distance and speed 1 Answer
Help Trying to translate cs to js problem with one block of code 2 Answers
stop object(door) translation 0 Answers