- Home /
Question by
greysonlanetemple · Apr 01, 2021 at 10:21 AM ·
2d2d-physics
Vector3.MoveTowards() Freezes players position
Hi! I've had some issues with "Vector3.MoveTowards(...)" where using it freezes the player to where it moved to, and doesn't allow for further movement. I've tried using a bool to turn on and off the function which holds the statement, but this appears to have no effect. I'm not too advanced in C#, and I am very grateful for any solutions anyone can provide. Thank you in advance!
This is my code
public class Player : MonoBehaviour
{
[SerializeField] public float moveSpeed = 5f;
public Rigidbody2D CharacterRB;
public Camera cam;
public Enemy closestEnemy = null;
Rigidbody2D rb;
Vector2 playerMovement;
public float speed;
Vector3 target;
Player player;
private void Start()
{
gameObject.GetComponent<DashKill>().enabled = false; //disables a different script called DashKill which is only enabled when the player dashes and hits an enemy
player = FindObjectOfType<Player>();
target = transform.position;
DashMovement = false;
}
// Update is called once per frame
void Update()
{
FindClosestEnemy();
Movement();
DashToClosestEnemy();
}
private void FixedUpdate()
{
CharacterRB.MovePosition(CharacterRB.position + playerMovement * moveSpeed * Time.fixedDeltaTime);
}
void Movement()
{
//Houses player movement
playerMovement.x = Input.GetAxisRaw("Horizontal");
playerMovement.y = Input.GetAxisRaw("Vertical");
Vector2 characterScale = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
characterScale.x = -1; //flips player based on direction
}
if (Input.GetAxis("Horizontal") > 0)
{
characterScale.x = 1; //flips player based on direction
}
transform.localScale = characterScale;
}
void FindClosestEnemy() //locates the nearest enemy and draws a line to them
{
float distanceToClosestEnemy = Mathf.Infinity;
Enemy[] allEnemies = GameObject.FindObjectsOfType<Enemy>();
foreach (Enemy currentEnemy in allEnemies)
{
float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
if (distanceToEnemy < distanceToClosestEnemy)
{
distanceToClosestEnemy = distanceToEnemy;
closestEnemy = currentEnemy;
}
}
Debug.DrawLine(this.transform.position, closestEnemy.transform.position);
}
void DashToClosestEnemy()
{
if(Input.GetKeyDown(KeyCode.X))
{
target = closestEnemy.transform.position;
target.z = transform.position.z;
gameObject.AddComponent<DashKill>().enabled = true; //enables the DashKill script
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); //this is the line I've been having all of the trouble with, as after using it, it no longer allows me to move anywhere else with the Movement() method
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613031600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to give a direction to a hook? 1 Answer
How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer
2D Platformer Character Controller 1 Answer
2D Teleporter Delay Issues 1 Answer
How to use Physics2D.OverlapCircleNonAlloc for ground check and jump 0 Answers