- Home /
Question by
jeremydulong · Aug 21, 2018 at 11:27 PM ·
positionvector3fixedupdatemovetowardsontrigger
MoveTowards sends object passed target
I'm using the very simple script below to move a cone object to a target location when the player touches a trigger. For some reason the cone flies towards the target and then continues on indefinitely and I can't figure out why. The target and trigger are parented to the the cone object, if that is relevant.
public class ConeObstacle : MonoBehaviour {
public Transform cone;
public Transform target;
private bool active = false;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player")){
active = true;
}
}
private void FixedUpdate()
{
if (active) {
cone.position = Vector3.MoveTowards(cone.position, target.position, 50 * Time.deltaTime);
}
}
}
Comment
Answer by greng5 · Aug 22, 2018 at 01:01 AM
If your target is parented to the object that is moving, the target will move the same distance, keeping the delta constant so it can never reach the target. Try unparenting the target from the cone