Pulling the player only towards one of multiple objects
So, I want to make a game where the player can click on different objects and be pulled towards them. To achive this, I used this code:
void FixedUpdate ()
{
if(Input.GetMouseButton(0))
{
// Move the player
Rigidbody2D playerRigBod2D = thePlayer.GetComponent<Rigidbody2D>();
playerRigBod2D.AddForce(transform.position - thePlayer.transform.position);
// Draw a line towards the player
GetComponent<LineRenderer>().SetPosition(1, thePlayer.transform.position);
}
}
This works fine with just one object of this prefab, but once I add more, the player gets pulled to the point in between these objects. I can't quite make out why, since transform.position
should be unique to each of the objects that have this script applied right?
I'd be thankful if one of you could shed some light on this, thank you :)
Here's some pictures, to show what's happening:
Your answer
Follow this Question
Related Questions
Vector question 0 Answers
Getting the correct angle for OverLapBoxAll 1 Answer
Why/How 2d tower of blocks collapse? 0 Answers
A 2D Character Controller with Rigidbody2D.AddForce 2 Answers
Getting rid of 2D collision jitter 1 Answer