Day 130 — Setting up a 3rd Person 3D Project

private CharacterController _controller;[SerializeField]
private float _speed = 6.0f;
[SerializeField]
private float _jumpHeight = 8.0f;
[SerializeField]
private float _gravity = 20.0f;
private Vector3 _direction;
private Vector3 _velocity;
void Start()
{
_controller = GetComponent<CharacterController>();
if (_controller == null)
{
Debug.LogError("Character Controller is null");
}
}
void Update()
{
if (_controller.isGrounded == true)
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
_direction = new Vector3(horizontal, 0, vertical);
_velocity = _direction * _speed;
if (Input.GetKeyDown(KeyCode.Space))
{
_velocity.y = _jumpHeight;
}
}
_velocity.y -= _gravity * Time.deltaTime;
_controller.Move(_velocity * Time.deltaTime);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store