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.
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 : MonoBehaviour
{
CharacterController controller;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
}
void OnCollisionEnter (Collision col)
{
controller.enabled = false; //this will disable your character controller
}
}
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.