How do I detect if a object touches another object?
Asked Answered
D

2

0

I’m fairly new to unity, so I’d like to apologize if this question sounds stupid. I’m trying to make pac-man in 3D as my first unity project and things have been going really smooth. (I’m almost done, in fact!) All I need to add is a way for the player (pac man) to detect if a ghost is touching it. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PacManMovement : MonoBehaviour 



{
    

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
         if (Input.GetKey(KeyCode.UpArrow)) 
        {
            // Moves an object forward, relative to its own rotation.
         transform.position += transform.forward * 4 * Time.deltaTime;

        }

         if (Input.GetKey(KeyCode.DownArrow))
        {
            // Moves an object down, relative to its own rotation.
         transform.position += -transform.forward * 4 * Time.deltaTime;

        }

         if (Input.GetKey(KeyCode.RightArrow))
        {
            // Moves an object right, relative to its own rotation.
         transform.position += transform.right * 4 * Time.deltaTime;

        }

         if (Input.GetKey(KeyCode.LeftArrow))
        {
            // Moves an object left, relative to its own rotation.
         transform.position += -transform.right * 4 * Time.deltaTime;
         
        }


        




    }    
}

Can someone please help me without changing the code I’ve already written?

Doorplate answered 18/6, 2023 at 19:30 Comment(0)
D
0

Ok so, to get this done, first watch the following layer mask tutorial:
https://youtu.be/vo8tByZIQ3k
You can add at the end of the Update() method, the following line

CheckForGhostCollision();

Then just below the Update() method, you add the following method

private void CheckForGhostCollision() {
     if (Physics.CheckSphere(transform.position, ghostCheckRadius, ghostLayerMask)) {
     Debug.Log("Touching one or more ghosts);
     }
}

You have also got to add the following variables

public float ghostCheckRadius = 1;
public LayerMask ghostLayerMask;

And last assign the ghostLayerMask to the ghosts (as you saw from the tutorial above) and to the script

Demos answered 18/6, 2023 at 19:42 Comment(0)
S
0

Unity has built-in function for this kind of thing. you can type OnCollisionEnter() below the update() function. Every time the script’s host collides with other objects Unity will do all the code inside of the OnCollisionEnter() method for once. you can test it through writing in debug.log(“man got touched by”+collision ). If there’s nothing happen,make sure you check the is trigger check box of the collider component in the inspector window .

Samirasamisen answered 29/3, 2024 at 13:54 Comment(1)

Can you show me how you would write "OnCollisionEnter" under the update() function? I've seen alot of people use this function which is why I'd be willing to try your idea first (also your idea is much more easier)

Doorplate

© 2022 - 2025 — McMap. All rights reserved.