[Unsolved] Rolling character

I’m trying to move my character (for example a perfect sphere or cylinder). And it moves. But I don’t want it to just slide across the ground. I want it to roll. I was thinking that I could animate the ball so it looks like its rolling but that would mess up my idea of the ball being able to roll over items if it were tall enough.

How I want it, is it looks like its rolling on its own. Like if I were moving it left and then I want to switch to right, it would slow down, stop a little for a 1/4 of a second, and then start rolling right.

Any idea how I could make a ball character roll instead of slide?

*Just across the X axis. (This is a sidescroller)


ANY INFORMATION (tutorials, codes, project files, or direction) is greatly appreciated and needed :slight_smile:

Thanks,
Brett

I’ve figured that:

When Left arrow is pressed, the character’s X-position value changes.
So then I was thinking of a way to change the character’s Z-rotation value at the same time.

But I can’t figure out how to do that.

Use transform.Rotate:

var speed = 10;

function Update(){
	transform.Rotate(Vector3(0,0,speed) * Time.deltaTime);
}

okay. It’s rotating now. But on its own. If I hit left it does nothing. If I hit right it flys up and right.
xD

If you are wanting to make a physics marble game, you may want to use AngularVelocity (mixed in with a little Velocity for air control).

This would allow you to add differing physical ground types like Ice or mud.

You would however have to take greater care for momentum, as you will continue rolling until stopped(although applying - Velocity to Velocity will help slow it down fast).

Must not be doing something right xD

Added the AngularVelocity lines. And it’s still not doing it.
Gr.

		if(Input.GetKeyDown(KeyCode.LeftArrow)) {
			}
		if(Input.GetKeyDown(KeyCode.RightArrow)) {
			}

That code seems to be the easiest. (Maybe)

Is there a way I can do a “rotate the Z axis at this speed” in each bracket?

EDIT: Maybe it will help with a visual
This is my character: http://itsbretter.com/Screen%20shot%202010-07-30%20at%2012.41.21%20PM.png
He moves side to side. but he needs to roll and not slide side to side.

found the code that i should be able to put in those brackets. That doesn’t work either.

Ntero’s code seems to sound the best. But i can’t get it to work :frowning:

Ok,

Try having a Player GameObject that contains a Rigidbody Component and a Custom Script like this:

using UnityEngine;
using System.Collections;

public class MyClass: MonoBehaviour {
    
static float moveSpeed = 100.0f;

Rigidbody rigid;

    void Start()
    {
        rigid = rigidbody;
    }

    void Update()
    {
      if(Input.GetKeyDown(KeyCode.LeftArrow)) {
         rigid.AddTorque (0, 0, moveSpeed);
         rigid.AddForce(moveSpeed, 0, 0);
         }
      if(Input.GetKeyDown(KeyCode.RightArrow)) {
         rigid.AddTorque (0, 0, -moveSpeed);
         rigid.AddForce(-moveSpeed, 0, 0);
         }
    }

}

The Speed and ratio between the two will need to be measured out and tested, as well as which axis will get it spinning the right way. Torque will increase the momentum and only modify momentum when touching something(and can will move you backwards if you hit something on the top of the marble), whereas straight Force will give you greater air control and faster turning.

http://www.tomabaird.com/?p=60
This is a quick sample of a similar effect in 3D. You’ll need XNA, but I used the mix of Torque and Force to create this effect (you’ll need XNA if you want to see it, sorry).

I get a lot of these errors when I use that code. It’s really bizarre.

Assets/Scripts/player_Rotate.js(1,6): UCE0001: ‘;’ expected. Insert a semicolon at the end.

When obviously there are semicolons at the end of the lines.

Delete the blank line at the top of the script file and have code in line one. Also make sure you return after the last bracket to ensure it reads the EOF character. Remove the script from your object and read it. I got some strange errors in my Unity that deleting the object with the scripts on it and remaking it in the level seemed to work at fixing things :?.

Did that

even tried retyping it in case it didn’t like the copy-paste.

bump xD

maybe it will help if i say that im using the 2D sidescroller prefab by TornadoTwin’s UnityPrefabs.com

bump?

Could you post a link to the ‘sidescroller’ prefab that you’re using? I checked the site, but wasn’t sure where to look for it.

As for your problem, it seems you have a couple of options: use ‘real’ physics to effect the rotation, or fake it.

To use real physics, you’d probably need to use a sphere collider, and use a joint or some code in your script to constrain the object to the xy plane. To move, you’d apply forces in the - and + x directions. You’d also need to set up your physics materials so that there was sufficient friction to cause the object to roll in a realistic manner.

Now, I’ve never tried doing anything like this with PhysX, so I can’t tell you whether this is really feasible (maybe someone else can comment on that). You could certainly give it a try though.

If that doesn’t work, another option would be to fake it. For this method, you’d also use a sphere constrained to the XY plane, but you’d use frictionless surfaces (and maybe disable rotation for the object as well).

Then, attach a child game object to the player object, and make this object the visual representation. In Update() or FixedUpdate(), measure the distance traveled since the last update. The direction of rotation can be determined from the direction of linear motion, and the amount of rotation (in radians) can be computed as ‘distance_traveled / player_radius’. You can then use the Transform.Rotate() function to apply the rotation, as described earlier.

If the player can jump or otherwise become ‘ungrounded’, you might want to detect that case and handle the rotation differently. For example, when the player is ungrounded, you could just keep the last computed angular velocity, or you could decrease the angular speed over time.

place this on your object with a rigidbody and use AddForce to move it. it should work.

function Update () 
{
transform.position.z = 0;
transform.rotation.x = 0;
transform.rotation.y = 0;
}

what i normaly do is create an enpty game object with the collider and rigidbody on it then place the mesh or plane mesh inside the game object that way you can rotate the mesh any angle to face the camera and will always roll sideways

Let me try it :stuck_out_tongue:

A bit confusing since I don’t know those codes :stuck_out_tongue:

http://www.unityprefabs.com/Product_CameraKit_SideScroller.html