I am a beginner of coding, I want the object “player” move to (7,0) from (-7,0). and I have no idea how to do it, pls help me!!!
I tried some by myself, the object “player” only move to the middle and stop. here is the code I made.
public class battleRule : MonoBehaviour
{
public GameObject player;
public GameObject enemy;
float playerSpeed = 10;
float enemySpeed = 5;
// Start is called before the first frame update
void Start()
{
player.transform.position = Vector2.MoveTowards(transform.position, new Vector2(7, 0), Time.deltaTime * 1);
}
// Update is called once per frame
void Update()
{
attack();
}
void attack()
{
if (playerSpeed > enemySpeed)
{
player.transform.position = Vector2.MoveTowards(transform.position, new Vector2(20, 0), Time.deltaTime * 1);
}
}
}
Do you want your character to move between two points continuously? i mean -7 to 7 to -7 to 7 ?
– Ercova