- Home /
"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!
Answer by e-bonneville · Mar 29, 2010 at 11:02 PM
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.
By the way, if this works for you, think about upvoting it so other people in your situation can find a (working) answer faster.
Answer by MooseTrap · Mar 30, 2010 at 06:55 AM
(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!
Code is more readable if you surround it with
[...]
tags. ;)
Ah cheers. I used , I didn't realise I needed
as well... No wonder it didn't look right!