I have created a basic script for walking and rotating the camera for my character controller. I now want to make the character run but cant get this to work.
I thought along the lines of checking to see if the character is grounded and if the b button on the controller is pressed to multiply my walk speed by a run speed variable.
like below
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class WalkAndRun : MonoBehaviour {
//Variables
public float walk_Speed = 5.0f;
public float run_speed = 2.0f;
public float side_Speed = 4.0f;
public float leftRight_Rotation_Sensitivity = 5.0f;
public float upDown_Rotation_Sensitivity = 5.0f;
public float upDownRange = 60.0f;
float verticalRotation = 0;
float verticalVelocity = 0;
CharacterController characterController;
// Use this for initialization
void Start () {
//Makes the mouse cursor disapear when testing the game.
Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//--Rotation--
//Look left and right.
//A float we name 'rotLeftRight' will use the axis of 'Mouse X' (the left and right) to...
float rotLeftRight = Input.GetAxis("Mouse X") * leftRight_Rotation_Sensitivity;
//...rotate around the 'y' axis. x=0, y=our float called 'rotLeftRight', and z=0.
transform.Rotate(0, rotLeftRight, 0);
//Look up and down.
//Caps the range we can look up and down.
verticalRotation -= Input.GetAxis("Mouse Y") * upDown_Rotation_Sensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
//Grab the main camera and its rotation
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//--Movement--
//Walk forwards and backwards, and left and right.
//Our forward speed comes from the Vertical axis.
float walkSpeed = Input.GetAxis("Vertical") * walk_Speed;
//Our side speed comes from our Horizontal axis.
float sideSpeed = Input.GetAxis("Horizontal") * side_Speed;
//And then use the above to create a Vector3 called 'speed' with x,y,z...
Vector3 speed = new Vector3( sideSpeed, verticalVelocity, walkSpeed );
//Our current speed is our rotation multiplied by our speed. This makes sure our character is moves in the right direction no matter the rotation.
speed = transform.rotation * speed;
//Run
if (characterController.isGrounded Input.GetButtonDown("joystick button 0")
walkSpeed = walk_Speed * run_speed;
//Gravity
verticalVelocity += Physics.gravity.y * Time.deltaTime;
characterController.Move( speed * Time.deltaTime );
}
}
forgive me if this is something really simple that I have/haven’t done but I am pretty new to unity and programming. Thanks.