- Home /
Object Rotation/Character Speed
I'm using the Script from the script reference to move the object found here - http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html - And i was wondering how it was that you speed up movement. I changed speed = 6.0F but to no avail, and i changed jumpspeed = 13.0F to change the jump but nothing changed.
How do i change the speed of movement, and how would i change the jump height?
Also,
How would i make an object rotate in the direction of movement? Like, around the axis of before i press the button so it's facing the direction i'm moving.
And, as a bonus, could you explain how each part of that movement script works? I don't understand how it gets what button you're pressing as all it has is is movedirection horizontal... I made my own script, but i was having a few problems with it, so here it is.
using UnityEngine; using System.Collections;
public class WASDMove : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey ("w"))
{
rigidbody.AddForce (Vector3.forward * 50);
}
if (Input.GetKey ("s"))
{
rigidbody.AddForce (Vector3.forward * 45);
}
if (Input.GetKey ("a"))
{
float translation = Time.deltaTime * 10;
rigidbody.AddForce (0,0,2);
}
if (Input.GetKey ("d"))
{
float translation = Time.deltaTime * 10;
rigidbody.AddForce (0,0,-2);
}
}
} Considering i use if (Input.Getkey ("key")) and that's what tells the program what to do when that is pressed, but the reference one is just horizontal and vertical with no input.
Lots of Love - Totality
Answer by aldonaletto · May 27, 2012 at 02:27 PM
1- You're having problems with the Editor's elephant memory: when you attach a script to some object and run, all variables exposed in the Editor (the public variables) have their initial values saved; these values are always loaded in the exposed variables when running, thus modifying their initial values in the script has no effect at all - you must change the values in the Inspector while not running. If you want to register changes made in the script, click the "gear" button at the left of the script component in the Inspector and select option "Reset".
2- The Move example uses keys AD to move the character left/right. These keys are associated to the default "Horizontal" axis in the Input Manager: it returns -1 when A or left arrow is pressed, 1 for D or right arrow, and 0 for none of them. It's a more convenient way to handle inputs: it's simpler, and makes control configuration menus easier to create (the same applies to keys WS and the "Vertical" axis).
3- You can use the mouse to rotate the character: add the script MouseLook.cs to it and select Mouse X in the Axes field. An alternative (Doom style) is to modify the script to use A and D to rotate the character:
C# version:
public float speed = 6.0f; public float jumpSpeed = 8.0f; public float turnSpeed = 90.0ff; // degrees per second public float gravity = 20.0;
// moveDirection is actually the moving velocity: Vector3 moveDirection = Vector3.zero; // cache the CharacterController to improve performance: CharacterController controller;
void Update() { // rotate the character with AD: transform.Rotate(0, Input.GetAxis("Horizontal") turnSpeed Time.deltaTime, 0); // set variable controller only once, instead of each Update: if (!controller) controller = GetComponent< CharacterController>(); if (controller.isGrounded) { // if character is grounded... // use WS to move it in its local forward/backward direction: moveDirection = Input.GetAxis("Vertical") transform.forward speed; // and check Jump (so it can only jump when grounded!):
if (Input.GetButtonDown ("Jump")) { // when Jump pressed... moveDirection.y = jumpSpeed; // apply a positive vertical speed } } // Apply gravity to the vertical speed: moveDirection.y -= gravity Time.deltaTime; // Move the controller with moveDirection speed controller.Move(moveDirection Time.deltaTime); }
JS version:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var turnSpeed : float = 90.0; // degrees per second var gravity : float = 20.0;
// moveDirection is actually the moving velocity: private var moveDirection : Vector3 = Vector3.zero; // cache the CharacterController to improve performance: private var controller : CharacterController;
function Update() { // rotate the character with AD: transform.Rotate(0, Input.GetAxis("Horizontal") turnSpeed Time.deltaTime, 0); // set variable controller only once, instead of each Update: if (!controller) controller = GetComponent(CharacterController); if (controller.isGrounded) { // if character is grounded... // use WS to move it in its local forward/backward direction: moveDirection = Input.GetAxis("Vertical") transform.forward speed; // and check Jump (so it can only jump when grounded!):
if (Input.GetButtonDown ("Jump")) { // when Jump pressed... moveDirection.y = jumpSpeed; // apply a positive vertical speed } } // Apply gravity to the vertical speed: moveDirection.y -= gravity Time.deltaTime; // Move the controller with moveDirection speed controller.Move(moveDirection Time.deltaTime); }
4- I didn't understand your script: are you using a CharacterController or a Rigidbody? Using both in the same object is a recipe for disaster (unless the rigidbody is kinematic, but you couldn't AddForce to it in such case). As a rule of thumb, only use rigidbodies to add a real world physics flavor to objects that need it: cannon balls, grenades, scene objects that may fall, bounce or hit other objects, like crates, barrels etc.
I'm using characterController, thanks for all the help! is there an if (Controller.isGrounded) = False; Or something of that nature, to control mid air jumping?
This if:
... if (controller.isGrounded) { // if character is grounded... ... if (Input.GetButtonDown ("Jump")) { // when Jump pressed... moveDirection.y = jumpSpeed; // apply a positive vertical speed } } ...only allows jumping when grounded. If you want to jump even while in mid air, move the second if outside the the first one.
No no, i mean when the person has pressed the button to jump, they will be able to move while in midair. As in: While grounded they can move at xSpeed and they can jump, and after jumping they can move at zSpeed (lower than xSpeed) until they hit the ground in which it will return to xSpeed. Would i do the same code without if (controller.isGrounded) so that it only uses the Grounded if it is, infact grounded?