So I just started making a game in Unity and was following a tutorial from a series by Sebastian Lague (Character Creation (E09: collisions and jumping) - YouTube) for a 3rd person game that was uploaded on Dec. 14, 2016. I used a Character Controller component on my player character and added on a script for collisions that worked in the video, but not for me. I’ve tried to search this up and seen people who have had some troubles with Character Controllers, but no answers. Yes, I could just use other methods of collision detection, but I would at least like to know why this used to work and no longer does (I have checked the script, nothing looks different than the video’s one except for variable values; if I am wrong about that, please correct me).
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float walkSpeed = 1;
public float runSpeed = 2;
public float turnSmoothTime = .25f;
float turnSmoothVelocity;
public float speedSmoothTime = .1f;
float speedSmoothVelocity;
float currentSpeed;
Animator animator;
Transform cameraTarget;
CharacterController controller;
void Start () {
animator = GetComponent<Animator> ();
cameraTarget = Camera.main.transform;
controller = GetComponent<CharacterController> ();
}
void Update () {
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero) {
float targetRotation = Mathf.Atan2 (inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraTarget.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool running = Input.GetKey (KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp (currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
Vector3 velocity = transform.forward * currentSpeed;
controller.Move (velocity * Time.deltaTime);
float animationSpeedPercentage = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat ("speedPercentage", animationSpeedPercentage, speedSmoothTime, Time.deltaTime);
}
}