- Home /
CharacterController problem
... Scripting. I wrote a short script to move the spacship in my game and rotate around. here it is,
var speed = 3.0; var rotateSpeed = 3.0; function Update () { var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed Input.GetAxis ("Vertical"); controller.SimpleMove(forward curSpeed); }
I added a character controler to my object but when I pressed play the ship didn't move fowards. The camera rotates properly but the ship only falls. how can i fix this?
Please title your questions in a way that are easy to find for others, and convey some information of content. "The usual Unity problem" says nothing to people who are looking at a list of questions and wondering if they're relevant to them.
Answer by e-bonneville · Jun 20, 2010 at 12:30 AM
You don't want to use simpleMove for this. Instead, use the more complex move command, the full documentation for which can be found here. Using this, you can opt as to whether you want to use gravity or not.
However, if you're trying to make a ship (as in Asteriods), this script may do the trick:
var rotateSpeed = 360.0; var thrustSpeed = 25.0;
function FixedUpdate() { transform.Rotate (Vector3.up (Input.GetAxis("Horizontal") rotateSpeed * Time.deltaTime) );
if (Input.GetButton("Thrust") ) { rigidbody.AddRelativeForce (Vector3.forward thrustSpeed Time.deltaTime); } }
Put it on a gameobject with a rigidbody and no character controller. To turn off gravity, just uncheck it in the rigidbody's properties. Good luck!
When I play the game there are no errors but the ship doesn't move fowards, it just rotates...
That's probably because your rigidbody is set to kinematic. Check it!
Answer by Tetrad · Jun 19, 2010 at 11:29 PM
Don't use a character controller for that. I don't think you can turn off gravity on a character controller. Instead, consider using a rigid body. Alternatively, put a collider underneath your ship to "run" on.
Also, hey look at this: http://www.unifycommunity.com/wiki/index.php?title=ShipControls
Answer by AnaRhisT · Jun 19, 2010 at 11:30 PM
the ship falls because charactercontroller has a gravity.. u can fix it for e.g. by using transform.Translate(direction.normalized * speed) or something like that