So i have this object moving to a vector3. How can i make it say overshoot it so it will keep moving in the same line(direction) but past this vector. here is what i Use.
transform.position = Vector3.MoveTowards(transform.position, target, speed*Time.deltaTime);
Thank you for your time.
Hi, you’d want to get a direction, then you can move in that direction using transform.Translate. At least that’s one way.
direction = target position - current position.
Here’s one that starts at the object’s position, moves it towards world 0,0,0 and keeps going in the same direction.
edit: Should probably normalize it too, which I forgot in the code below.
using UnityEngine;
public class MoveTheThing : MonoBehaviour
{
public Vector3 direction;
void Start()
{
Vector3 start = transform.position;
Vector3 target = new Vector3(0, 0, 0);
direction = (target - start);
}
void Update()
{
transform.Translate(direction * Time.deltaTime);
}
}
mopthrow:
Hi, you’d want to get a direction, then you can move in that direction using transform.Translate. At least that’s one way.
direction = target position - current position.
Here’s one that starts at the object’s position, moves it towards world 0,0,0 and keeps going in the same direction.
edit: Should probably normalize it too, which I forgot in the code below.
using UnityEngine;
public class MoveTheThing : MonoBehaviour
{
public Vector3 direction;
void Start()
{
Vector3 start = transform.position;
Vector3 target = new Vector3(0, 0, 0);
direction = (target - start);
}
void Update()
{
transform.Translate(direction * Time.deltaTime);
}
}
that was it Thank you some times the littlest things can drive me crazy to figure it out Lesson learned
yep, you should
direction = (target - start).normalized;
Somehow it is off the exact point and moves in a straight line but not getting over the exact Vertor3 any ideas why ?
Are you comparing two Vector3 for equality? Like
Vector3 targetPosition;
if(transform.position == targetPosition)
{
Debug.Log("hit")
}
This will likely never work. Vector3 contain 3 float variables. Floats are imprecise.
You need to test if it is close enough instead.
if(Vector3.Distance(transform.position, targetPosition) < 0.01f)