Unity Collision Event

nananana·2022년 9월 26일
0

Youtube link

Thank you creator

I tried some of the collision events. Didn't go creative with this one because I am tired.

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

public class BallsJustBalls : MonoBehaviour
{
    Rigidbody rb;
    bool JumpPressed;
    bool isGround;
    bool isSpin;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        // rb.velocity = Vector3.right;
        // rb.velocity = Vector3.left;
        JumpPressed = false;
        isGround = false;
        isSpin = false;
        // rb.velocity = new Vector3(2,4,3);
        // rb.AddForce(Vector3.up * 50, ForceMode.Impulse); //modes: Accesleration, Force, Impulse, VelocityChange
    }

    void Update(){
        if(Input.GetButtonDown("Jump")){
            JumpPressed = true;
        }
        if(transform.position.y<0.28){
            isGround = true;
        }else{
            isGround = false;//This method not ideal. Probably use collision based? maybe another way would exist.
        }//Method is also very much model dependent. Not flexible

        if(Input.GetKey(KeyCode.Q)){
            isSpin = true;
        }else{
            isSpin = false;
        }
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //Rigid body related fnc. is recommended to be placed in Fixed Update
        Debug.Log("update");
        // rb.AddForce(Vector3.right);

        Debug.Log(transform.position.y<0.28);

        if(JumpPressed&isGround){
            rb.AddForce(Vector3.up * 5, ForceMode.Impulse);
            JumpPressed = false;
        }
        if(isSpin){
            rb.AddTorque(Vector3.up * 3, ForceMode.Impulse);
        }
        

        Vector3 v = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical") );
        rb.AddForce(v, ForceMode.Impulse);
    

    }

    private void OnTriggerStay(Collider o){
        if(o.gameObject.name == "Cube"){
            rb.AddForce(Vector3.up * 2, ForceMode.Impulse);
        }
    }
}

This is code written last time with an additional trigger functions. Maybe this is how damage is given to the player based on a region. Like a fire or something.

However I do remember these things often was annoying since it is hard to recognise the hitbox or something. Still interesting.

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

public class Events : MonoBehaviour
{
    // Start is called before the first frame update
    MeshRenderer mesh;
    Material mat;
    void Start()
    {
        mesh = GetComponent<MeshRenderer>();
        mat = mesh.material;
    }

    private void OnCollisionEnter(Collision c){
        if(c.gameObject.name == "Sphere"){
            mat.color = new Color(0,0,0);
        }
    }

    private void OnCollisionStay(Collision c){

    }

    private void OnCollisionExit(Collision c){
        mat.color = new Color(1,1,1);
    }
}

Collision events for the objects. Collision was done basically on other balls where they would change color.

All these things are interesting but I really can't formulate a way how they could be used correctly.

Collision is the source of a million bugs (I think)

For example any buggy game throwing you through walls or into the sky usually do this through to collision physics.

League of Legends have Azir, which was really buggy due to his collision physics in the ultimate.

Possibly the reason is the assumption.

Designers assume that a collision between objects should be done between the surface. However in certain cases (in most buggy games) objects don't collide on the surface but they seem to overlap for some reason.

Collision calculation in this situation gets buggy resulting in odd behavior.

Good games prevent any overlapping of seem to work around such events (like I'd ever know)

Still it was a honor to meet a famous source of bugs.

0개의 댓글