This script is attached to Player object:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (BoxCollider))]
public class PlayerController : MonoBehaviour {
public float walkSpeed = 6;
public float runSpeed = 10;
public float strafeSpeed = 5;
public float gravity = 20;
public float jumpHeight = 2;
public bool canJump = true;
private bool isRunning = false;
private bool isGrounded = false;
public bool IsRunning
{
get { return isRunning; }
}
void Awake () {
GetComponent<Rigidbody>().freezeRotation = true;
GetComponent<Rigidbody>().useGravity = false;
}
void FixedUpdate () {
// get correct speed
float forwardAndBackSpeed = walkSpeed;
// if running, set run speed
if (isRunning) {
forwardAndBackSpeed = runSpeed;
}
// calculate how fast it should be moving
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal") * strafeSpeed, 0, Input.GetAxis("Vertical") * forwardAndBackSpeed);
targetVelocity = transform.TransformDirection(targetVelocity);
// apply a force that attempts to reach our target velocity
Vector3 velocity = GetComponent<Rigidbody>().velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.y = 0;
GetComponent<Rigidbody>().AddForce(velocityChange, ForceMode.VelocityChange);
// jump
if (canJump && isGrounded && Input.GetButton("Jump")) {
GetComponent<Rigidbody>().velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * jumpHeight * gravity), velocity.z);
isGrounded = false;
}
// apply gravity
GetComponent<Rigidbody>().AddForce(new Vector3 (0, -gravity * GetComponent<Rigidbody>().mass, 0));
}
void Update() {
// check if the player is touching a surface below them
checkGrounded();
// check if the player is running
if (isGrounded && Input.GetButtonDown("Sprint")) {
isRunning = true;
}
// check if the player stops running
if (Input.GetButtonUp("Sprint")) {
isRunning = false;
}
}
void checkGrounded() {
/* ==============
* REMEMBER
* ==============
* If you change the size of the prefab, you may have
* to change the length of the ray to ensure it hits
* the ground.
*
* All obstacles/walls/floors must have rigidbodies
* attached to them. If not, Unity physics may get
* confused and the player can jump really high
* when in a corner between 2 walls for example.
*/
float rayLength = 0.7f;
RaycastHit hit;
Ray ray = new Ray(transform.position, -transform.up);
//Debug.DrawRay(ray.origin, ray.direction * rayLength);
// if there is something directly below the player
if (Physics.Raycast(ray, out hit, rayLength)) {
isGrounded = true;
}
}
}
There are some parts in the script it's using "Sprint" For example:
// check if the player is running
if (isGrounded && Input.GetButtonDown("Sprint")) {
isRunning = true;
}
// check if the player stops running
if (Input.GetButtonUp("Sprint")) {
isRunning = false;
}
But "Sprint" is not defined in the editor input: Edit > Project Settings > Input:
I can change the size in the Input Manager to 19 and it will duplicate the Cancel so I changed the name to Sprint. But what should be the config for the Sprint ? It's now the Cancel config. Config I mean the settings for each property.
When running the game before added the Sprint I'm getting this exception:
ArgumentException: Input Button Sprint is not setup. To change the input settings use: Edit -> Project Settings -> Input PlayerController.Update () (at Assets/My Scripts/Character1/PlayerController.cs:62)
My question is what settings should I set for the Sprint in the Input Manager ?