- Home /
The question is answered, right answer was accepted
3rd person character controller not responding correctly
Hello everyone,
I have been following a tutorial on you tube trying to learn more about 3rd person character controller. In did everything he did in the videos but yet, my character controller does not want to jump or it just slowly floats to the surface.
I thought i found the solution in the jump function, but it wasnt there. Im still a student in coding so thats why i cant figure out whats wrong here.
Anyone here who can help me out with this?
I have unity version 5.1.1f1
My setup is that i have the camera controller on the main camera, and the charactercontroller is on a cube.
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
[System.Serializable]
public class MoveSettings
{
public float forwardVel = 12;
public float rotateVel = 100;
public float jumpVel = 25f;
public float distToGrounded = 0.01f;
public LayerMask ground;
}
[System.Serializable]
public class PhysSettings
{
public float downAccel = 0.75f;
}
[System.Serializable]
public class InputSettings
{
public float inputDelay = 0.1f;
public string FORWARD_AXIS = "Vertical";
public string TURN_AXIS = "Horizontal";
public string JUMP_AXIS = "Jump";
}
public MoveSettings moveSetting = new MoveSettings ();
public PhysSettings physSetting = new PhysSettings ();
public InputSettings inputSetting = new InputSettings ();
Vector3 velocity = Vector3.zero;
Quaternion targetRotation;
Rigidbody rBody;
float forwardInput, turnInput, jumpInput;
public Quaternion TargetRotation{
get { return targetRotation; }
}
bool Grounded()
{
return Physics.Raycast (transform.position, Vector3.down, moveSetting.distToGrounded, moveSetting.ground);
}
// Use this for initialization
void Start () {
targetRotation = transform.rotation;
if (GetComponent<Rigidbody> ())
rBody = GetComponent<Rigidbody> ();
else
Debug.LogError ("The character needs a rigidbody");
forwardInput = turnInput = jumpInput = 0;
}
void GetInput(){
forwardInput = Input.GetAxis (inputSetting.FORWARD_AXIS);
turnInput = Input.GetAxis (inputSetting.TURN_AXIS);
jumpInput = Input.GetAxisRaw (inputSetting.JUMP_AXIS);
}
// Update is called once per frame
void Update () {
GetInput ();
Turn ();
}
void FixedUpdate(){
Run ();
JumP ();
rBody.velocity = transform.TransformDirection (velocity);
}
void Run(){
if (Mathf.Abs (forwardInput) > inputSetting.inputDelay) {
//Move
velocity.z = moveSetting.forwardVel * forwardInput;
}
else
velocity.z = 0;
}
void Turn(){
if (Mathf.Abs (turnInput) > inputSetting.inputDelay) {
targetRotation *= Quaternion.AngleAxis (moveSetting.rotateVel * turnInput * Time.deltaTime, Vector3.up);
}
transform.rotation = targetRotation;
}
void JumP(){
if (jumpInput > 0 && Grounded ()) {
//jump
velocity.y = moveSetting.jumpVel;
} else if (jumpInput == 0 && Grounded ()) {
//zero out our velocity.y
velocity.y = 0;
} else {
//decrease velocity.y
velocity.y -= physSetting.downAccel;
}
}
}
And the camera controller is exactly the same as in the video, but il add the code here too, so you can copy, paste and test it for yourself with the same setup.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Transform target;
public float lookSmooth = 0.09f;
public Vector3 offsetFromTarget = new Vector3(0, 6, -8);
public float xTilt =10;
Vector3 destination = Vector3.zero;
CharacterController charController;
float rotateVel = 0;
// Use this for initialization
void Start () {
SetCameraTarget (target);
}
void SetCameraTarget(Transform t)
{
target = t;
if (target != null)
{
if (target.GetComponent<CharacterController>())
{
charController = target.GetComponent<CharacterController>();
}
else
{
Debug.LogError("The camaera target needs a characterController");
}
} else
Debug.LogError ("Your camera needs a target");
}
void LateUpdate(){
//moving
MoveToTarget ();
//rotating
LookAtTarget ();
}
void MoveToTarget(){
destination = charController.TargetRotation * offsetFromTarget;
destination += target.position;
transform.position = destination;
}
void LookAtTarget(){
float eulerYAngle = Mathf.SmoothDampAngle (transform.eulerAngles.y, target.eulerAngles.y, ref rotateVel, lookSmooth);
transform.rotation = Quaternion.Euler (transform.eulerAngles.x, eulerYAngle, 0);
}
}
Answer by Nirish-Alakhramsing · Oct 08, 2015 at 09:10 PM
After searching the web i found someone with almost the same problem. I am using a cube with a box collider. Now the distance to the ground was set to 0.1 and that caused it to hardly move forward or to jump.
So i played around somewhat and now i can walk and jump again. So im closing this subject.