Add constant force to a sphere?

I’ve managed to try a few ways to add a constant force to a sphere, but none of them appear to easily allow the force to change directions base on collisions. Any ideas as to how I can implement this?

I have a square terrain, with a single cube on each side. I want the sphere to roll in an initial direction, then ‘bounce’ off one of the cubes as they collide then move constantly in a different direction based off where on the cube the ball collided.

One thing you could try would be always to apply the force in the direction of the velocity. Presumably when the sphere bounces the velocity will be adjusted accordingly, meaning that the applied force will then ‘push’ the sphere in the new direction.

(Note that I haven’t tried this myself, so I can’t say for sure that it would work.)

I tried that, but now the sphere goes crazy and flies off the terrain. Any ideas as to what I’m doing wrong here?

using UnityEngine;
using System.Collections;

public class BallBehaviour : MonoBehaviour {
	// Use this for initialization
	void Start () {
		constantForce.force = -Vector3.forward * 5;
	}

	// Update is called once per frame
	void Update () {
		constantForce.force = rigidbody.velocity * 5;
	}
}

Hm, without trying it myself, I’m not really sure. I will point out though that physics updates (adding forces and so on) should usually go in FixedUpdate() rather than Update(). Also, I’d recommend normalizing the velocity (when it has sufficient magnitude) to yield a unit-length direction vector, and then scaling that direction vector by the desired force magnitude; this will give you more control of the applied force. (I don’t know if that will fix the problems you describe, but those are the things I’d try first.)

Thanks. The sphere seems to be much more controlled now. Although, now I need to edit the other game objects so that the sphere doesn’t knock them over or go through them so I can actually get a better picture of what’s going on, but as far as this thread goes I think my problem is solved. :slight_smile: