Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3D Collision with character controller

i'm new to unity and i'm trying to make an endless runner. When my player (a ball) hits one of the walls the game needs to go the scene: LostMenu. My problem is that the collision doesn't work. Nothing happens when they collide... Here are the inspectors on both the player and the walls: Walls http://prntscr.com/a2pgzm Player http://prntscr.com/a2ph66 .

The collision script on the walls:

using UnityEngine;
using System.Collections;

public class LostByWallCSR : MonoBehaviour
{
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "Player")
        {
             Application.LoadLevel("LostMenu");
             Debug.Log ("WORKS!");
        }
   }
}

The movement script on the player:

using UnityEngine;
using System.Collections;

public class CharacterControllerz : MonoBehaviour {

public float speed = 5f;
public float gravity = 20f;
private Vector3 moveDirections = new Vector3();
private Vector3 inputs = new Vector3();

void FixedUpdate()
{
    CharacterController cc = GetComponent<CharacterController>();
    if (cc.isGrounded) 
    {
        if (Input.GetKey("left"))
            inputs.z = 3;
        else if (Input.GetKey("right"))
            inputs.z = -3;
        else
            inputs.z = 0;

        moveDirections = transform.TransformDirection(inputs.x, 0, inputs.z) * speed;                    
    }

    inputs.x = 5;
    moveDirections.y = inputs.y - gravity;
    cc.Move(moveDirections * Time.deltaTime);
}
}

Any idea on how to setup the collision properly? Attaching a rigibody is not helping it. Unless i'm missing something ofcourse.

like image 929
BelgianWizard Avatar asked Jun 13 '26 14:06

BelgianWizard


1 Answers

Make sure both objects have a collider component.

You need to attach a rigidbody component to one of the objects.(if you don't attach rigid body it will not work)

your Sphere collider is Trigger , remove that or change your function to

public void OnTriggerEnter(Collider other)
    {

    }
like image 110
AminSojoudi Avatar answered Jun 17 '26 23:06

AminSojoudi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!