So I have an object and I want it to move towards a vector, whose coordinates I stored as a float (lets call it destiny). I want the object to move towards that vector every step, but only if two conditions are met.
I searched for some function that let me move an object every step, but I just found transform.Translate (Vector3.direction). As you see, I need the movement to be a little more specific.
I know there’s a function in JS called MoveTowards, but I’m using C#. I searched for a code for moving objects and I found something about coroutines, and I’m a little bit afraid of jumping onto that.
I don’t have any code, as I couldn’t find a way to move the object, but I can still give you an idea of what I got:
Thanks! I’ve searched for Vector2.MoveTowards, the code doesn’t give me any errors, but the object doen’t move. I think that what is actually moving is just the Vector, not my object. Is that possible?
OK it seems I figured out how to move my object, but it’s not working as it should. I don’ know why, but the object doesn’t move gradually towards my vector, it just jumps there. But the worst thing is that it jumps towards a vector really far away from it’s origin. It doesn’t make sense, as the object is a child of another and uses small coordinates like 1 or 2. Help please! This is my code right now:
Vector2 posStart = new Vector2(-0.4f,0.4f);
Vector2 posEnd = new Vector2(0f,0f);
public float movSpeed = 1;
void Update()
{
if (Input.GetKey("f"))
{
transform.position = Vector2.MoveTowards(posStart,posEnd,movSpeed);
}
}
Here, posStart is where my object is and posEnd is the vector it should move towards to. every step “f” is pressed it should move towards that vector.