Isn't There A "Not A Method" Way Of Detecting For Collision?

code:

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

public class MoveOut : MonoBehaviour
{
    private MeshCollider mc;

    // Start is called before the first frame update
    void Start()
    {
        mc = GetComponent<MeshCollider>();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.layer == 6)
        {
            Vector3 oldEuler = transform.eulerAngles;
            transform.LookAt(Camera.main.transform);
            while ()//colliding
            {
                transform.position += transform.forward * 1f;
            }
            transform.eulerAngles = oldEuler;
        }
    }
}

You can’t loop like that in Unity.

You could loop (and yield in a coroutine of course!) somewhere else and wait for the impact such as by watching a bool get set to true in your callback.

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

I changed my code a bit in the post now…

The Collision structure has the information necessary to resolve a collision. That is, for each ContactPoint where colliders have sunk into each other, there is a point at which they overlap, and a separation distance which is the amount of overlap whenever negative. A clever loop could calculate the necessary average direction and distance to try to get them un-colliding… which is actually what the physics engine does.

Do we want rid of the collision method?

Because Raycast can work. But it is not collider accurate! So you couldn’t roll a bunch of balls down a hill in a realistic way unless you did lots and lots of raycast.

But what you could do is Raycast from center of an object out through it’s verts from direction center towards the vert and out* if the Raycast hits then wow that vert must have intersected. But it’s totally only suitable for some symmetrical primitives. Such as a ball rolling down a hill (I must try this now)

But aside that if collider shape was never a concern you just wanted your object to be away from other objects by some buffer then just a few Raycast in common directions may suffice.

There are other ways like comparing two colliders but infrastructure would be required to know which two colliders to compare infrastructure could be anything from Raycast to using a tile check system.

t

thanks, this sounds like what I need!

I am not sure what your question means but instead of your while loop you can try to use
OnCollisionStay (Unity - Scripting API: Collider.OnCollisionStay(Collision))

If you need a more “manual” approach with simple colliders you could take a look at what Physics has to offer (OverlapSphere, SphereCast, ClosesPoint etc.) Unity - Scripting API: Physics