I watched this awesome guide from 3dbuzz where they explained how to make third person character controller system. It all worked fine and everything but when i added gravity in my script…my character started to move
really fast…which was a bummer since im not making a superhero game
i watched the episode again and again but could not spot any differences in my actions…so if anyone could point
for me what might be the problem in my script i would appreciate it.
Here’s TP_Controller.cs
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public static CharacterController CharacterController;
public static TP_Controller Instance;
void Awake ()
{
TP_Camera.UseExistingOrCreateNewCamera();
CharacterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
}
void Update ()
{
if (Camera.main == null)
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomotionInput()
{
float deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
{
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
}
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
{
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
}
and here’s TP_Motor.cs
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour
{
public static TP_Motor Instance;
public float MoveSpeed = 10f;
public float Gravity = 21f;
public float TerminalVelocity = 20f;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Awake ()
{
Instance = this;
}
public void UpdateMotor ()
{
SnapAlignCharacterWithCamera();
ProcessMotion();
}
void ProcessMotion()
{
// Transform our move vector into world space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize movevector if magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// multiply movevector by movespeed
MoveVector *= MoveSpeed;
// Reapply vertical velocity MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply Gravity
ApplyGravity();
// move character via the new vector we now have
TP_Controller.CharacterController.Move(MoveVector);
}
void ApplyGravity()
{
if (MoveVector.y > -TerminalVelocity)
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if (TP_Controller.CharacterController.isGrounded && MoveVector.y < -1)
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
void SnapAlignCharacterWithCamera()
{
if (MoveVector.x != 0 || MoveVector.z != 0)
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.main.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
}