- Home /
Disabling character controller after collision
Hi there
Quick question... and perhaps an easy one for the more experienced. I am new to unity and I am trying to figure out a way to disable the character controller on my FPS player after collision with another game object.. like a cube with a box collider.
I found this piece of code which is attached to my cube:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisablingController : MonoBehaviour
{
CharacterController controller;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
controller = GetComponent<CharacterController>();
controller.detectCollisions = false;
}
}
Which I guess should work.. but doesn't. I am a scripting noob and I would really appreciate some help.
Thank you.
after collision with another game object
You need to detect collision first. So both your character and the object character collides with needs to have a collider attached to them. Then turn your script into this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisablingController : $$anonymous$$onoBehaviour
{
CharacterController controller;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
}
void OnCollisionEnter (Collision col)
{
controller.enabled = false; //this will disable your character controller
}
}
Answer by ThomasOr · Dec 15, 2019 at 10:51 AM
Have a look at https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
You can pop that function into a script that is on the same gameObject that has your characters controller on it and you can disable the controller when it is called.