- Home /
The character controller goes through objects
I am kinda new in Unity and I have a problem in the character controller. So I made a simple practicing game. I made a movement code for my player That is the code :
CharacterController controller;
public float speed = 6f;
public float gravity = 20f;
public float jump = 8f;
Vector3 moveDirection = Vector3.zero;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, 2);
moveDirection *= speed;
if(Input.GetButton("Jump"))
{
moveDirection.y = jump;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
And I made a cube which should be the barrier the player hits and lose so I gave that barrier this code :
private void onTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
and I made that wall's IsTrigger property true. But when I run the game the player doesn't lose and restart the scene but it goes through the barrier like it is nothing. So can you help me solve this problem? Thanks in advance.
Is the character controllers IsTrigger property also set to true? Comment out your current OnTriggerEnter code and just add a simple debug.log("Triggered") statement to deter$$anonymous$$e whether the trigger is setup correctly.
The IsTrigger property is set to true in both the player and the barrier. but the debug.log("Triggered") didn't print anything that means that there is a problem in the trigger right ?
According to the API documentation (read the note)...https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
I suggest unchecking the IsTrigger property on the player's collider and adding a RigidBody component to the player gameobject if you haven't done so already. I hope this gets the triggers activating.
Answer by supertemich2005 · Aug 19, 2020 at 06:09 PM
I am not fluent in unity at all, i've tried it only once and it was an eternity ago, so my answer may be wrong or something, but afaik, imho, iirc and iiar(if i am right), try attaching rigid body and controlling rigid body.
Rigid body i used to make collision boxes, so your object would, well, collide, have gravity, etc.
rigidbody and character controllers on the same gameobject is not recommended as you would have some weird movement. A rigidbody controller for your player should be designed instead or you follow some tutorials on how to set up your character controller correctly
Answer by blagwedge · Aug 20, 2020 at 12:06 AM
Does the object with the CharacterController have a collider?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Peek Definition not working in Visual Studio Code for C# tokens 1 Answer
My cooldown method is not working the cube move only once then It won't move anymore 1 Answer
Project .sln Generates Incorrectly (Contains Duplicate Projects). 1 Answer
.csproj not being created 1 Answer