Search Unity

Question Player not colliding with tile map. SOLVED

Discussion in '2D' started by Coolboy300, Sep 27, 2020.

  1. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    I used a script to control the player and it isn't colliding with the tile map, but when I use a rigid body 2D the player collides with the tile map. Is there some setting that I have to turn on? Version 2020.1.3f1
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Collision is based on layers. You may have moved the tilemap to another layer that isn't setup to collide with the layer the player is on. Other possibility I can think of is the tilemap collider is set to trigger.
     
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    What's wrong with using a Rigidbody2D? Just turn it to Kinematic if you dont want Gravity affecting it
     
  4. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    The problem is something with the script. I tried using Kinematic with the script but it still wont collide
     
  5. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    They're both on the default layer at the top of the inspector near the tag. Also i made sure its not on trigger cause the rigidbody will collide. It must be something with the script
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Show your script and make sure to use the code tags:
    upload_2020-9-28_14-35-26.png
     
  7. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    Code (CSharp):
    1.     using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.     public float MoveUp;
    8.     public float MoveDown;
    9.     public float MoverRight;
    10.     public float MoveLeft;
    11.     public float MoveUpFaster;
    12.     public float MoveDownFaster;
    13.     public float MoverRightFaster;
    14.     public float MoveLeftFaster;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.        
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetKey("w"))
    26.         {
    27.             transform.position += Vector3.up * MoveUp;
    28.         }
    29.  
    30.         if (Input.GetKey("a"))
    31.         {
    32.             transform.position += Vector3.left * MoveLeft;
    33.         }
    34.  
    35.         if (Input.GetKey("s"))
    36.         {
    37.             transform.position += Vector3.down * MoveDown;
    38.         }
    39.  
    40.         if (Input.GetKey("d"))
    41.         {
    42.             transform.position += Vector3.right * MoverRight;
    43.         }
    44.  
    45.         if (Input.GetKey("space"))
    46.         {
    47.             if (Input.GetKey("w"))
    48.             {
    49.                 transform.position += Vector3.up * MoveUpFaster;
    50.             }
    51.  
    52.             if (Input.GetKey("a"))
    53.             {
    54.                 transform.position += Vector3.left * MoveLeftFaster;
    55.             }
    56.  
    57.             if (Input.GetKey("s"))
    58.             {
    59.                 transform.position += Vector3.down * MoveDownFaster;
    60.             }
    61.  
    62.             if (Input.GetKey("d"))
    63.             {
    64.                 transform.position += Vector3.right * MoverRightFaster;
    65.             }
    66.         }
    67.  
    68.     }
    69. }
    I also made a similar moving script in bolt. I also heard about something called a playerController or something like that.
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    So the major thing here i see is that you are moving your character via Transform instead of applying forces to the Rigidbody2D.

    See this video for a better explanation, but basically if you are using a Rigidbody2D (aka you want physics to be applied, do not move your character by the transform).

     
  9. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    I also have a bolt script shall I send an image of how it looks?
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    You can but i've never used a visual scripting tool before. Shouldnt be too hard for me to understand. But the basics you should know are whether to move something via Transform or via AddForces (if rigidbody).
     
  11. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    At the bottom i was trying to use the add force but i wasn't sure how i could do it. I also tried using velocity but the sprite moved through the walls still
     

    Attached Files:

    • pic.png
      pic.png
      File size:
      291.1 KB
      Views:
      416
  12. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    I just noticed something strange. If i use kinematic it won't collide but if i use dynamic it collides? I know i can change the gravity but i dont want the entire sprite rotating. Is there a way to stop that?
     
  13. Coolboy300

    Coolboy300

    Joined:
    May 6, 2020
    Posts:
    10
    wait actually i found out that you use freeze rotation. The player is now colliding. Issue is solved :)