Greetings.
Firstly I have searched through a few different forums for an answer to this problem, and have not found an answer to this issue
I am having issues with applying gravity to a third person controller. I have added a rigidBody to my controller and without gravity the controller works fine, plays the animations no issue, but as soon as I add gravity it falls through my world into infinity.
I have placed both a cube with a box collider and a plane to act as a floor, and I have also added a cube at the same level. the cube hits my ‘floor’ and stay there, but my third person controller just falls.
here is the code (extensively commented as as I am providing this to students)
#pragma strict
//Public Variables
//the speed we are moving (Walking) at, set at 3 in Unity
var walkSpeed:float;
//the speed we are moving (Walking) at, set at 3 in Unity
var runSpeed:float;
//uses the mouse to rotate/turn the character.
//set at 110 in Unity
var rotationSpeed:float;
//variable to put the Animation speed into, so animations will play at the speed we are moving at
var walkAnimSpeed:float;
//variable to put the Animation speed into for strafe movement, so animations will play at the speed we are strafing at
var strafeAnimSpeed:float;
//Private Variables
//as it is private Unity cannot access it from the inspector
private var charController:CharacterController;
//Boolean flags to track what the player object is doing and play idle animation, Unity3D does not need to access these
private var isWalking:boolean;
private var isRunning:boolean;
private var isStrafing:boolean;
function Start () {
//initialise the charController variable
charController = GetComponent(CharacterController);
//initalise our isWalking and etc flags
isWalking = false;
isRunning = false;
isStrafing = false;
}
function Update () {
//if statement to determine which button is pressed
//set the sensativity gravity of the Input vertical horizontal to 1000
if(Input.GetAxis("Vertical") > 0){
//Flag walking
isWalking = true;
Debug.Log("Up/Forward");
//check to see if we are running (holding down left shift)
//the GetButton.Shift reflects what we called it in the Input settings in Unity
if (Input.GetButton("Shift")){
//Flag running
isRunning = true;
//Animation speed
animation['run'].speed = runSpeed;
//Play the walk animation
animation.CrossFade("run");
Debug.Log("Running");
charController.Move(transform.forward * runSpeed * Time.deltaTime); //forward * speed per second (not speed per frame)
}
else {
//Animation speed
animation['walk'].speed = walkAnimSpeed;
//Play the walk animation
animation.CrossFade("walk");
//use the charController variable to move the character (lowercase t retrieves the transform from unity)
charController.Move(transform.forward * walkSpeed * Time.deltaTime); //forward * speed per second (not speed per frame)
}
}
else if(Input.GetAxis("Vertical") < 0){
//Flag walking
isWalking = true;
Debug.Log("Down/Backwards");
//check to see if we are running (holding down left shift)
//the GetButton.Shift reflects what we called it in the Input settings in Unity
if (Input.GetButton("Shift")){
//Flag running
isRunning = true;
//Animation speed
animation['run'].speed = runSpeed;
//Play the walk animation
animation.CrossFade("run");
Debug.Log("Running");
charController.Move(transform.forward * -runSpeed * Time.deltaTime); //forward * speed per second (not speed per frame)
}
else {
//Animation speed, negative as we are playing the animation backwards, only runs for one "time unit" then time hits 0.
animation['walk'].speed = -walkAnimSpeed;
//Play the walk animation
animation.CrossFade("walk");
//use the charController variable to move the character (lowercase t retrieves the transform from unity)
charController.Move(transform.forward * -walkSpeed * Time.deltaTime); //negative forward * speed per second (not speed per frame)
}
}
// no run speed applied to strafe, because it's a bit silly
else if(Input.GetAxis("Horizontal") > 0){
//Flag Strafe
isStrafing = true;
Debug.Log("Right");
//Plays strafe animation, if the animations do not exist comment this out
animation['strafe_R'].speed = strafeAnimSpeed;
//Play the walk animation
animation.CrossFade("strafe_R");
//end comment out here
//use the charController variable to move the character (lowercase t retrieves the transform from unity)
charController.Move(transform.right * walkSpeed * Time.deltaTime); //right * speed per second (not speed per frame)
}
else if(Input.GetAxis("Horizontal") < 0){
//Flag Strafe
isStrafing = true;
Debug.Log("Left");
//Plays strafe animation, if the animations do not exist comment this out
animation['strafe_L'].speed = strafeAnimSpeed;
//Play the walk animation
animation.CrossFade("strafe_L");
//end comment out here
//use the charController variable to move the character (lowercase t retrieves the transform from unity)
charController.Move(transform.right * -walkSpeed * Time.deltaTime); //negative right * speed per second (not speed per frame)
}
// any other case (ie, 0) means the player is not moving on the horizontal or vertical axis
else {
isRunning = false;
isWalking = false;
isStrafing = false;
}
//end of the if statement
//if the player is not walking, strafing or running, we are idle
if(!isWalking !isStrafing !isRunning){
animation.CrossFade("idle");
}
//when mouse moves, rotate camera player
transform.Rotate(Vector3(0, Input.GetAxis("Mouse X") *rotationSpeed * Time.deltaTime, 0));
}