El1mination
El1mination

Reputation: 1

Movement stops whenever player touches ground

I am trying to learn the basics of unity collision and I want the movement of the player to stop whenever it touches an obstacle but the movement stops whenever it touches the ground. I tagged the obstacle with a tag called obstacle and left the ground untagged but whenever the player touches the ground it stops all movement. Does anyone know how to fix this? Heres my code: `using UnityEngine;


public class PlayerCollision : MonoBehaviour
{
public Movement playerMovement;

void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log(collisionInfo.collider.tag == "Obstacle");{
playerMovement.enabled = false;
}

}

}

Upvotes: 0

Views: 86

Answers (1)

Snapper
Snapper

Reputation: 3

The code you've written stops the movement of the player whenever its collides with something. The code wouldn't even compile because of the "{" at the end.

void OnCollisionEnter(Collision collisionInfo)
{
  // returns true or false in the console depending on whether player its colliding with the obstacle
  // code won't compile because of the "{" at the end
  Debug.Log(collisionInfo.collider.tag == "Obstacle");{ 
  playerMovement.enabled = false;
}

To fix that you can use an if statement and remove the "{" at the end like shown below.

It is also recommended to use CompareTag when working with tags.

    void OnCollisionEnter(Collision collisionInfo)
    {
      if(collisionInfo.collider.CompareTag("Obstacle"))
      {

         playerMovement.enabled = false;

      }
    }

Upvotes: 0

Related Questions