I can't get the target position in Vector3.MoveTowards to update.
Hey, i'm rather new to Unity so i apologize if the answer is pretty obvious but, i can't get the movetowards to update its target position, in context, i have an enemy object and i'm trying to make it move towards the player, to do so i'm trying to make it so the target position updates along with the player position, here's the script:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enem_Camin : MonoBehaviour {
public GameObject enemigo;
public float speed;
public GameObject jugador;
private Vector3 origen, atacar;
// Start is called before the first frame update
void Start()
{
speed = 5f;
}
// Update is called once per frame
void Update()
{
atacar = jugador.GetComponent<Movimiento>().posicionActual;
float moverse = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, atacar, moverse);
}
}
atacar is using a component in the player's script called actual position that updates every frame, but the moveTowards function is only getting the first frame and then stopping with the update, how can i fix this? thanks in advance.
Answer by chor64 · Jun 01, 2020 at 11:13 PM
You should put a high value when using MoveTowards, like 80f or 100f
I've tried but that's the speed that makes the most sense in the size of the scene as a whole, since any higher speeds make it look like a blur passing by from the speed, thanks but, i don't really understand how higher movement speed would change a different variable that is the target of said movement.
It's because you're multiplying by Time.deltaTime, a value that is usually around 0.01f, so to get your desired speed, try multiplying it by 100f. ((5f 100 = 500f) 0.01 = 5f). But, don't remove Time.deltaTime, because it makes sure that your speed is consistent in all machines.
Tried it, nothing changed other than the enemy moving faster now, but thanks for attempting to help!