- Home /
Character Controller collisions on older version no longer working?
So I just started making a game in Unity and was following a tutorial from a series by Sebastian Lague (https://www.youtube.com/watch?v=qITXjT9s9do) 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);
}
}
The character simply goes right through objects like it had no collision system.
Did you remember to add a collider on the object you want the character controller to stand on?
If you're talking about the objects I want collisions with, it works great once I added box or mesh colliders (depending on objects)! Do you by any chance know why it worked without them in the video? If I saw something wrong though, please correct me. I was using the "Prototyping" models in the Unity Standard Assets.
Answer by tormentoarmagedoom · Sep 18, 2017 at 08:00 AM
Good day @SpinoRex !
I don't know if something changed or the update does this, but i had the same problkem with some collisions. I had to "remake" some of the colliding objects. After finishing i was thinking, and realized was a RigidBody problem.
Read again the OntriggerEnter definitiuon, and check if there is/isn't a RigidBody where it should be
Please: Mark the answer, Upvote it, and when solved, close the question.
PD: If need more help, jkust ask using @tormentoarmagedoom
Bye ! :D
Your answer
Follow this Question
Related Questions
Problem with collision detection after upgrading to Unity 2019.2.9f1, 0 Answers
How to Check if you are Colliding with more than one object with the same tag? 2 Answers
Why is my trigger colliding with another trigger? 2 Answers
Enemy Ai Damage gets done multiple times 0 Answers
Bullets go through collider even with continuous collision detection! 0 Answers