Rolling ball keeps it's rotation after rolling down a hill???

I’m new to Unity and JavaScript. I’m making a pretty common 3D Platform game where the player controls a marble and basically needs to get from point A to point B. The issue I’m having right now is if I let the ball roll down a hill without touching any of the controls, the ball seems to keep it’s current speed once it reaches a flat area and keeps going without slowing down.

I’m sure this is a pretty simple issue to fix and I’d like it more if somebody could more of just point me in the right direction rather than just giving me the code. :slight_smile:

This is what I have so far:::

if(canPlay)
	{
		Screen.lockCursor = true;
		Screen.showCursor = false;
		var moveForward = Input.GetAxis("Vertical") * speed;
		var moveSideways = Input.GetAxis("Horizontal") * speed;
		
		var camForward = Camera.main.transform.TransformDirection(Vector3.forward);
		camForward.y = 0;
		camForward.Normalize();
		
		var camSideways = Camera.main.transform.TransformDirection(Vector3.right);
		camSideways.y = 0;
		camSideways.Normalize();
		
		moveForward *= Time.deltaTime;
		moveSideways *= Time.deltaTime;
		
		transform.Translate(camForward*moveForward,Space.World);
		transform.Translate(camSideways*moveSideways,Space.World);
         }

Does your ball have a rigidbody attached to it? If so, you could try increasing the value of the rigidbody’s Angular Drag.

That’s a physics problem…you should take advantage of Unitys built in engine.

Okay. That slows it down. But if I try to move in the opposite direction while it’s slowing down or get to the top of the hill on flat ground it still continues to spin. After setting the angular drag that did stop if from rolling, but how would I go about fixing the rotation when I reach the top of a hill or try to control the ball while it’s in the middle of rolling down a hill?

It seems while it’s trying to roll down the hill on it’s own and I try to control it up the hill, it appears to be fighting with itself. It never seems to work normally until I wait for the ball to stop rolling on it’s own.

I’m still having a hard time with this… I can’t seem to decided if I should be using a rigidbody or CharacterController… I’ve tried both, but I can’t seem to get the physics down. If I use a rigidbody I just about have everything working. Gravity works and the ball rolls down hill and when it’s not moving I can control where it goes. The only problem I’m having when using a rigid body is if I try to control the ball while it’s rolling down a hill it almost appears to be fighting itself. Or for example if I try to control the ball while it’s slowing down(angular drag) I can move the ball around, but once I let go of the buttons the ball continues it’s angular drag until it stops.

So I decided to try out the character controller. Using the character controller doesn’t seem to make the ball roll down the hill. So could anybody help me with this? :smile: If I go with the rigidbody it almost appears I need to find some way to counteract the angular drag when a button is pushed so it can make it up hills.

This is what I have so far:::

var speed = 10;
var gravity = 20;

private var move = Vector3.zero;
private var previousPosition : Vector3;
private var circ : float;

function Start()
{
	circ = 2 * Mathf.PI * collider.bounds.extents.x;
	previousPosition = transform.position;
}

function Update () 
{
	var controller = GetComponent(CharacterController);
	
	if(controller.isGrounded)
	{
		move = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
		move = Camera.main.transform.TransformDirection(move);
		move *= speed;
		move.y = 0;
		if(Input.GetKey("space"))
			{
				move.y = 8;
			}
	}
	
	move.y -= gravity * Time.deltaTime;
	controller.Move(move * Time.deltaTime);
}

function LateUpdate()
{
	var movement : Vector3 = transform.position - previousPosition;
	
	movement = Vector3(movement.z,0,  -movement.x);
	
	transform.Rotate(movement / circ * 360, Space.World);
	
	previousPosition = transform.position;
}

I think the problem you are running into is you are using a hybrid system. The rigidbody controls how gravity and natural forces affect your ball, which is why it rolls down the hill without you having to code that part explicitly. However, your movement code just calls transform.Translate (from the first code snippet you put up) to move.

If it were me, I would definitely stick with the rigidbody instead of the character controller. Instead of using transform.Translate though, I’d use a combination of rigidbody.AddTorque and rigidbody.AddForce. That will force physics to control the ball instead of you needing to do it manually.

Sweet. Thanks for pointing me in the right direction MegadethRocks! :stuck_out_tongue: Turned out the AddForceAtPosition solved all my problems!