Search Unity

Question How can i keep the player rolling instead of hitting on object and slowing down?

Discussion in 'Scripting' started by superdoesthings, May 19, 2023.

  1. superdoesthings

    superdoesthings

    Joined:
    May 17, 2023
    Posts:
    13
    I want to make the player not stop and hit the food and slow down, then clip through it, I want the player to instantly go through the food and a point be given (code below). I want the food to visually disappear after it gets hit, too.

    collision script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class collide : MonoBehaviour
    {
    void OnCollisionEnter (Collision collision)
    {
    if (collision.collider.tag == "lazer"){
    Debug.Log("hit");
    //add respawn later

    }
    if (collision.collider.tag == "food"){
    Destroy(collision.collider);
    //add a point
    }
    }
    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Triggers work like collisions but without involving physics. Basically collision detection without handling.

    You could handle your food with triggers. For that you need the collider to the set to isTrigger=True. Also, try abstracting from your problem to be better able to search for solutions. Effectively you want to collect items. That is done in millions of games, so there is hundreds of tutorials on it, like this one:



    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
    https://docs.unity3d.com/Manual/Tags.html

    Also, there are collision layers for similar what-collides-with-what usecases, but that's more helpful for generally ignoring collisions with certain objects
    https://docs.unity3d.com/Manual/LayerBasedCollision.html

    In the future please use code tags to include code examples.
     
    Last edited: May 19, 2023