I have a player that moves around my game normally, and I have an object that has a radius around it that’s meant to pull the player into its center to damage it. I’m moving the player using rb.MovePosition. I can get the player to move normally, and I can get the object to pull the player in, but I can’t get both things to happen at the same time. Basically I want the player to be pulled in, but only slightly. I want them to be able to escape the pull. How do I make two opposing forces act on the same object.’
velocity.x = joystick.Horizontal;
velocity.y = joystick.Vertical;
rb.MovePosition(transform.position + velocity * speed);
if(attracted == true){
tractorDirection = tractorBeam.transform.position - transform.position;
}
//Trying to add both the tractor direction and the velocity of the player, and to make the force towards the tractor beam less than the input velocity force.
Hey, try this:
velocity.x = joystick.Horizontal + pullVelocity.x;
velocity.y = joystick.Vertical + pullVelocity.y;
Where your pullVelocity is
pullVelocity = Amplitude of Pull x Direction of the Pull
Honestly I haven’t implemented this kind of gravity, but have you tried something like:
var tractorDirection = Vector3.zero;
if(attracted == true){
var tractorHeading = tractorBeam.transform.position - transform.position;
tractorDirection = tractorHeading / tractorHeading.magnitude;
}
tractorGravity = someValueLessThanSpeed;
velocity.x = joystick.Horizontal;
velocity.y = joystick.Vertical;
rb.MovePosition(transform.position + velocity * speed + tractorDirection * tractorGravity);
If tractorGravity is less than speed, it should be possible to escape by moving directly away from it. But you can play around with the value until you have what you’re looking for.
That vector subtraction gives you a direction with a magnitude(sometimes called a heading), so you have to divide out the magnitude for just the actual direction.
But if someone with more 2D physics experience can correct me, feel free. Hope that helps!