- Home /
Question by
Ciphon_Gaming · May 04, 2021 at 11:43 AM ·
movementvector2
How to make enemy move towards a x position
Hi. I am a beginner programmer and I am currently making a game for high school project. In this particular project I want to make a player dodge obstacles. I have already done the first obstacle which just move down wards normally. But I want to make a smart enemy where when the player presses the k key the player would move to the right but the enemy would also move to right. I used blackthornprod's code to make a smooth transition when it is moving positions but instead of following the player the enemy always goes to middle of the screen then it will move accordingly. How do I fix this? The script is below: (It is inside a update function)
Vector2.MoveTowards(transform.position, targetPos, transitionSpeed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.S) && transform.position.x <= -16f)
{
targetPos = new Vector2(transform.position.x - XIncrement, transform.position.y);
chanceToMove--;
}
if (Input.GetKeyDown(KeyCode.K) && transform.position.x <= 16f)
{
targetPos = new Vector2(transform.position.x + XIncrement, transform.position.y);
chanceToMove--;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613033248im_/https://answers.unity.com/themes/thub/images/avi.jpg)