- Home /
 
               Question by 
               Obi-Wan_Jabroni · Apr 13 at 10:31 PM · 
                movementaddforcechase  
              
 
              Does ClampMagnitude override AddForce?
I have a script that uses Vector2.ClampMagnitude which allows an enemy to chase my player around. When the enemy takes damage, I am using Rigidbody2D.AddForce() to knock him away, but this part of my code isn't working. I am wondering if ClampMagnitude is interferring with it.
  void Update(){
         //Chases player
         body.velocity = Vector2.ClampMagnitude(player.position - transform.position, speed);
         //Stops the enemy infront of the player
         if(Vector3.Distance(transform.position, player.position) <= range){
             tooClose = true;
         }else{
             tooClose = false;
         }
 
         if(tooClose == true){
         body.velocity = Vector3.zero;
         }
 
 public void TakeDamage(int damage){
         if(player.facingRight){
             body.AddForce(-transform.right * thrust, ForceMode2D.Impulse);
             health -= damage;
             Debug.Log("Hit right");
         }else{
             body.AddForce(transform.right * thrust, ForceMode2D.Impulse);
             health -= damage;
             Debug.Log("Hit left");
         }
     }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
AddForce Movement Constraint 0 Answers
Movement with AddForce: Wrong Direction 2 Answers
,How to change the direction of an already moving GameObject 2 Answers
Top Down Movement with AddForce() 1 Answer
AddForce to Camera 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                