Moving rigidbody with addforce to certain position

Hello :slight_smile:

I am trying to make a rigidbody(pusher) move back and forth using addforce.

The reason for the add force is so that objects on top of the rigidbody move, instead of freeze like using .translate, etc.

I want the pusher to go from start (0,0.005,-12) [empty game object at this location] to end (0,0.005,-8) [empty game object at this location] but once it hits the end, to go back to the start, over and over. The other thing to take into account is, that the object count on top of the pusher can vary. I want the pusher to move at a certain speed instead of being dependent on the amount of weight on the pusher.

Here is what I have so far.

Thank you to all who assist on this, as it’s been 3 hours of no luck searching for a real answer. :slight_smile:

I had to pull out a good bit of the old code to make it a better base.

using UnityEngine;
using System.Collections;

public class SlidingPusher : MonoBehaviour
{

	public Rigidbody Pusher;
	public Transform start;
	public Transform end;
	


	void Start() {

	
	}
	void FixedUpdate() {
	

if (Pusher.rigidbody == (end.position))

		{
						
Pusher.rigidbody.AddForce (start.position, 10);
		}
		
		
						
	}
}

you should try something like this:

	public Transform pusher;
	public Vector3 start;
	public Vector3 end;

	void Start() {
	}

	void FixedUpdate() {
		if ( pusher.transform.position == end){
			pusher.rigidbody.velocity = Vector3.zero;
			pusher.rigidbody.angularVelocity = Vector3.zero;
			pusher.rigidbody.AddForce((start - end) * 10);
		}else if (pusher.transform.position == start){
			pusher.rigidbody.velocity = Vector3.zero;
			pusher.rigidbody.angularVelocity = Vector3.zero;
			pusher.rigidbody.AddForce((end - start) * 10);
		}
	}

Since your question is 2years old, forgive me that I just overflew all the posts.

If I get it correctly we got the same problem,

I am using addforce on my rigidbody instead of transform.position, because the latter doesn’t trigger physic-events on the way.
But I need to stop the rigidbody at a certain position no matter the costs!

well my workaround was to toggle a check on your current position after the force is applied
and reset your position every frame, as soon as the positioncheck matches your desired position.

but than you have to find some condition that ends the positionreset!!!
in my case I could do it on any collisionenter :slight_smile:

another way might be time or setting up triggers wherever this may occur in your game

best wishes

kayb14