x


"wheel" character

Hi guys,

Just wondering what the best method would be, for controlling a character that is basically just a wheel, in a 2.5D environment.

My aim is to have it move in two dimensions (move left and right whilst rolling, fall down, but not move away or towards camera), and if possible be affected by physics.

I've been able to achieve basic functionality using the capsule collider, this gives me the basic movement, but no rolling, and no real physics.

I've been reading tutorials such as the 2d Game Tutorial, and the 3D platformer tutorial, but I'm having trouble determining what my workflow should be.

Cheers in advance!

more ▼

asked Mar 29 '10 at 10:10 PM

MooseTrap gravatar image

MooseTrap
89 6 7 16

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If you're using a capsule collider, you can rotate the wheel via script. If you want cool physics, add a rigidbody, which uses PhysX. Here's a script that'll rotate your wheel:

var speed = 5;

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

Simply put this on your wheel, and it should rotate. Another approach is to add a Wheel Collider, and rotate it using this script:

var speed = 5;

function Update () {
    collider.motorTorque = speed;
}

You can add WheelColliders from the Physics option under Add Component. Create an empty GameObject. Then, add a WheelCollider and this script to it. Move the wheelcollider to your wheel, and make it a child if the wheel. Note: Wheelcolliders do not do the graphics for you. What you want to do, if you're using the wheelcollider is to remove the collider from your wheel, and put the first script on it. That will ensure it rotates, but doesn't do any physics or interact with the terrain. Hope this helps!

P.S. This doesn't do any of the character controlling for you - just the physics. If you want the wheelspeed to be modifiable, try using an if input statement to detect which arrow is pressed, and to increase the speed based on that.

more ▼

answered Mar 29 '10 at 11:02 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

By the way, if this works for you, think about upvoting it so other people in your situation can find a (working) answer faster.

Mar 30 '10 at 12:50 AM e.bonneville

Unless, of course, it works but you don't like it.

Mar 30 '10 at 12:51 AM e.bonneville
(comments are locked)
10|3000 characters needed characters left

(code wouldn't fit in comment)

Cheers. I'm having a bit of trouble getting this to work though? Where should this code be? I've made a movement script for the character, attached a rigidbody, and included the first code snippet in the script. My object hierarchy is: Game Object - Using this as container, script is attached here) Player Prefab - No script or animation Weapon with Mouse-Aim Probably (definitely!) a n00b error.. I'm very new to scripting. Code is posted below:


var speed = 5;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function Update () {
    if (grounded) {
        // We are grounded, so recalculate movedirection directly from axes
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
        moveDirection = -transform.TransformDirection(moveDirection);
        moveDirection *= speed;
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;
    transform.Rotate(Vector3.right * Time.deltaTime * speed);
}

 

Is there a way to restrict the player's rotation? At the moment the character is ever-so-slowly pivoting towards the camera.

I read something about "Configurable Joints". Is this something I should look into for this type of character..? I believe this was used in the 2d platform tutorial for the rocket. If this should be a new question I can repost, but I think it ties into this one.

Thank you for your help!

more ▼

answered Mar 30 '10 at 06:55 AM

MooseTrap gravatar image

MooseTrap
89 6 7 16

Code is more readable if you surround it with

 [...] 
tags. ;)
Mar 30 '10 at 07:07 AM qJake

Ah cheers. I used , I didn't realise I needed

 as well... No wonder it didn't look right!
Mar 30 '10 at 07:30 AM MooseTrap
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1873
x1044
x1036
x24

asked: Mar 29 '10 at 10:10 PM

Seen: 1353 times

Last Updated: Mar 29 '10 at 10:10 PM