- Home /
How do I add a pulling force?
So, my character is a ball, and you solve puzzles by pushing cubes, (and more) but if you push a cube or ball into a wall, you can't move it. So I thought of adding a pulling thing, where is I press a button, it pulls objects to me, my problem, is that I can't/don't know how to do this. So could anyone help me with this?
Answer by mbro514 · Jul 03, 2020 at 06:20 PM
Assuming that the cubes have dynamic rigidbodies, first find the direction from the cube to the ball like this (assuming that the cube that is to be pulled is stored in a GameObject variable called "cube"):
if (Input.GetButton("Pull")){
Vector3 direction = transform.position - cube.transform.position;
//And then we give the direction a length of 1
direction = direction.normalized;
}
Next you would move the cube towards the ball like this (assuming that it's stored in a variable called "cubeRb"):
cubeRb.AddForce(direction * 20);
The 20 can be changed to whatever number is needed, and this line of code needs to follow after the "direction = direction.normalized;" line. Anyway, I hope that this helps!
Your answer
Follow this Question
Related Questions
My sphere loses velocity when jumping 2 Answers
Fireing a tank projectile. 1 Answer
Unity Physics On Input Issue? 0 Answers
How do I add a pulling force? 1 Answer