- Home /
How do I make a tractor beam affect my player?
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.
Answer by HappiiGamer · Oct 16, 2019 at 04:24 AM
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
Happy to hear that! If you want like a gravity pull that gets weaker the farther you are, you can change that Amplitude value based on the distance from your tractor beam. Good luck with your project!
Answer by GrayLightGames · Oct 16, 2019 at 04:39 AM
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!
Your answer
Follow this Question
Related Questions
Game Object is moving towards the left when fire button is pressed. 2 Answers
how to have air control after a rocket jump? 0 Answers
Butcher-like blood physics and rendering 0 Answers
Basically how to make a fan in Unity2D 1 Answer
Trigger in child - boxcollider2d without trigger on parent issues. 0 Answers