Having problems having two objects recognize when they collide
I'm trying to make a game where to objective is to make the player get from point A to pint B in a maze and once you get to pint B you touch a cube and its supposed to stop the players movement and transition into a small sequence that says "You Win!"(Still haven't made the transition) but nothing seems to be working. Please keep in mind that I'm very inexperienced and both my "End Cube" and player only have Box Colliders and not rigid body since activating it makes the whole thing spin out of control.
Heres my "Player Code" using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
Me trying to end the movement when the two objects meet the player meets object tagged "END" using UnityEngine;
public class CollideEnd : MonoBehaviour{
public ThirdPersonMovement Movement;
void OnControllerColliderHit(ControllerColliderHit col)
{
if (col.collider.gameObject.tag == "END")
{
Movement.enabled = false;
}
}
}
Any help would be greatly aprecciated thank you!
Answer by RealFolk · Jan 27, 2021 at 01:45 PM
Just add a rigidbody component to your gameobject tagged "END" and click the checkbox in the inspector that says isKinematic. I would also mark its box collider isTrigger in the inspector. Then add this code on the ThirdPersonMovement script.
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "END")
{
Movement.enabled = false;
Debug.Log("End the level");
}
}