-2
\$\begingroup\$

I'm a beginner at coding and wanted to make a quick little program to test my googling skills so far. I'm trying to make the player GameObject be given a speed boost when it collides with the "speed" GameObject and it doesn't work (the cube can still move but it isn't affected when colliding with the speed). Below is my player movement script:

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

public class Player : MonoBehaviour
{
    public float moveSpeed = 10f;
    void Update()
    {
        Vector3 position = transform.position;
        if (Input.GetKey("w")){
            position.y += moveSpeed * Time.deltaTime;
        }
        if (Input.GetKey("s")){
            position.y -= moveSpeed * Time.deltaTime;
        }
        if (Input.GetKey("d")){
            position.x += moveSpeed * Time.deltaTime;
        }
        if (Input.GetKey("a")){
            position.x -= moveSpeed * Time.deltaTime;
        }
        transform.position=position;
    }
}

And here is the power-up script. I used a Debug.Log() to figure out where the problem is at the line under the private void:

using UnityEngine;

public class Speed : MonoBehaviour
{
    public float speedIncrease = 5f;
    
    private void OnTriggerEnter2D(Collider2D collision){
        if (collision.tag == "Player"){
            GameObject player = collision.gameObject;
            Player playerScript = player.GetComponent<Player>();
            if (playerScript){
                playerScript.moveSpeed += speedIncrease;
            }
        }
    }
}

I come from Blender Stack Exchange where we provide all pieces of info we feel are relevant. Please let me know if that's not the case here or if I left something out.

EDIT here is what the objects look like in the inspector

the first one is player and the second one is the power up enter image description here

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Make sure there is a rigidbody on the object receiving the collision, and make sure the IsTrigger is checked in the collision box that sent the collision. \$\endgroup\$
    – Mangata
    Jun 18 at 9:09
  • \$\begingroup\$ Remember to use CompareTag() over .tag == \$\endgroup\$
    – DMGregory
    Jun 18 at 9:49
  • \$\begingroup\$ did both of those and it still dosent work \$\endgroup\$
    – desperrrr
    Jun 18 at 15:50

2 Answers 2

1
\$\begingroup\$

Your scripts are okay, your issue is due to scene setup rather than coding.

In order to detect a trigger collision, OnTriggerEnter2D() has some requirements:

  • Both objects must have one Collider2D Component.
  • At least one Collider2D Component must be a trigger (Is Trigger field in the Inspector, or isTrigger property via code).
  • At least one of the (two) objects must have a RigidBody2D Component attached to it.

The reason you need a RigidBody2D is, trigger collisions are computer during the Fixed Update when the physics update is run as well.

Then, you must add a RigidBody2D to either object before being able to detect the collision. If you don't need actual physics for your GameObjects, you can simply tick the Is Kinematic field of the RigidBody2D Component in the Inspector: this will prevent your object from moving due to gravity, forces and impulses

Moreover, don't worry about additional undesired physics behaviour: RigidBody2D + Collider2D behaves like a "solid" object; whereas RigidBody2D + trigger Collider2D detects collisions but doesn't affect self or others' movement (unless coded otherwise by yourself).


Edit

As stated in the documentation:

A Kinematic Rigidbody 2D does not collide with other Kinematic Rigidbody 2Ds or with Static Rigidbody 2Ds; it only collides with Dynamic Rigidbody 2Ds.

This means that you can't use the current setup in your scene, as both objects are kinematic. You need to remove the RigidBody Component or make it not kinematic for (at least) one of the objects.

A different approach is discussed in this Unity Forum thread. You can enable additional collision pairs (e.g. Kinematic-Kinematic) by configuring Project Settings > Physics > Contacts Pair Mode to "Enable All Contacts". It is also recommended moving your kinematic bodies using Rigidbody.MovePosition() and Rigidbody.MoveRotation() from FixedUpdate.

\$\endgroup\$
9
  • \$\begingroup\$ i did all of that and its still not working. both objects have a rigidbody 2d and both have colliders based on their different shapes with the power up collider having is trigger checked. and both rigidbodys are kinematic with continuous collision detection \$\endgroup\$
    – desperrrr
    Jun 18 at 14:39
  • \$\begingroup\$ also the debug.log dosent work when i put it under the private void in the power up script. does that mean that the code dosent work? \$\endgroup\$
    – desperrrr
    Jun 18 at 15:57
  • \$\begingroup\$ Kinematic bodies are not intended to react to collision, because they model objects moving differently from strict physics laws (e.g. player characters). Unity doesn't support kinematic bodies + continuous collision detection, so that could be the reason the collision is still not detected in your project. I tested an MVP using Discrete rather than Continuous CD, and OnTriggerEnter2D() was correctly invoked. \$\endgroup\$
    – liggiorgio
    Jun 18 at 16:13
  • \$\begingroup\$ so should i use static or dynamic \$\endgroup\$
    – desperrrr
    Jun 18 at 16:15
  • \$\begingroup\$ Try both, and see what happens. They may behave the same, or they may have some differences. Test them both, and choose the one that satisfies you most. If in doubt, read about their differences in the documentation. If you want more control over collisions, you can use a kinematic RigidBody and perform the collision check with CircleCast() or other similar functions. \$\endgroup\$
    – liggiorgio
    Jun 18 at 16:18
0
\$\begingroup\$

I fixed the issue. I needed to give my player object a new tag called "Player"enter image description here

New contributor
desperrrr is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .