- Home /
Question by
BTEC_Australian · Sep 05, 2014 at 05:38 PM ·
speed
How do change my characters speed?
Hi there I am making a survival game where I want to be able to change the speed of my character when I click a certain button, how can I do this? I don't know where the script goes because the character controller already has its own speed scripts. Please help!
Comment
you need to do control over Inside Vectore3 value by coding.
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Answer by JusticeAShearing · Sep 12, 2014 at 05:22 PM
You could just make your own movement script like I do, where you control the speeds and such things.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var rotateSpeed : float = 3.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
//Animation Code Here
if (controller.isGrounded)
{
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
//Rotation Code
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButtonUp("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
//Gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move Controller
controller.Move(moveDirection * Time.deltaTime);
}
Then, you must make the speed variable a static variable, affected by other scripts. Then, code the GUI Button to access the static variable and edit it how you want to.
if (GUI.Button (new Rect (10, 70, 200, 40), "Main Menu" ))
{
Movement.health += /*However much you want added*/;
//The plus could be changed with a minus, a star for multiplication or a forward slash for division
}