Using direction and Rigidbody2D.AddForce() to move towards object.


I’ve got an issue using the new Rigidybody2D.Addforce function. Im creating a 2d program, and am working on the enemy AI. I’m trying to add force towards the player when they are far away, and away from the player when they are too close. The direction seems to calculate fine and works with lerp (see commented lines) functions, but I need to use a rigidbody2D to simulate colliding and stop things going through walls. Each enemy has a rigidbody2D and circle collider, each with a mass of 1 (I messed around with this number to no avail).

In this current state, the enemies can be pushed around but refuse to be affected by the force that I presume is affecting them.The function below is called in update, and has been attempted from fixed update, with no changes. Thanks for any assistance!

Vector2 heading = this.transform.position - player.transform.position;
		double distanceVector = heading.magnitude;

		//determine direction towards player
		Vector2 dir = player.transform.position-transform.position;
		dir = dir.normalized;

		//overlap circle from enemy searching for player
		inRange = Physics2D.OverlapCircle(this.transform.position, 0.5f, 1 << LayerMask.NameToLayer("Player"));

		Vector2 lastKnown =;

		//Raycast to nearby player to determine line of sight.
		if(inRange == true)
			RaycastHit2D hit = Physics2D.Raycast(this.transform.position, dir);

			if(hit.collider != null)
				if(hit.collider.tag == "Player")
					//if enemy is out of weapon range and out of retreat range, move towards player
					if(distance < reference.getAdvanceRange() && distance > reference.getRetreatRange())
						//this.transform.position = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y), dir, 0.2f * Time.deltaTime);
						this.GetComponent<Rigidbody2D>().AddForce(dir, ForceMode2D.Force);
						lastKnown = new Vector2(player.transform.position.x, player.transform.position.y);
					//if too close to player, move back
					else if(distance < reference.getRetreatRange())
						//this.transform.position = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y),-dir, 0.2f * Time.deltaTime);
						this.GetComponent<Rigidbody2D>().AddForce(-dir, ForceMode2D.Force);
						lastKnown = new Vector2(player.transform.position.x, player.transform.position.y);
					lastKnown = player.transform.position - this.transform.position;
				//moves to last known player location
				//this.transform.position = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y), lastKnown, 0.2f * Time.deltaTime);
				this.GetComponent<Rigidbody2D>().AddForce(lastKnown, ForceMode2D.Force);
			//handles rotation of enemy
			lookPosition = player.transform.position;
			this.transform.rotation = Quaternion.LookRotation(Vector3.forward, lookPosition - this.transform.position);

The first parameter to AddForce is supposed to be a force vector – you’re passing in a position instead. That doesn’t make sense.

If you want to apply a force in the direction of the player, try something like this:

Vector2 dir = lastKnown - (Vector2)(player.transform.position);
GetComponent<Rigidbody2D>().AddForce(dir * 1000, ForceMode2D.Force);

Play with the strength of the force (1000 in the example above) until it seems right.