Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help Needed] player can walk through walls

Discussion in '2D' started by xwarflecx, Aug 1, 2018.

  1. xwarflecx

    xwarflecx

    Joined:
    Sep 28, 2017
    Posts:
    2
    btw, i am using tilemap collisions cause i made a tilemap, and for the player i have put a box collider and 2d rigidbody, heres my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour {
    7.  
    8.  
    9.     private float axisX;
    10.     private float axisY;
    11.     private Rigidbody2D rb;
    12.  
    13.     public Slider healthBarRef;
    14.  
    15.     public float jumpHeight;
    16.     public int speed;
    17.  
    18.     public float health;
    19.     // Use this for initialization
    20.     void Start () {
    21.         speed = 200;
    22.         jumpHeight = 300.0f;
    23.         rb = GetComponent<Rigidbody2D>();
    24.        
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update () {
    29.  
    30.  
    31.  
    32.         axisX = Input.GetAxis("Horizontal");
    33.  
    34.         transform.Translate(new Vector3(axisX, 0) * speed * Time.deltaTime);
    35.  
    36.         if(Input.GetKeyDown(KeyCode.Space)) {
    37.  
    38.             rb.velocity = new Vector3(0f, jumpHeight, 0f);
    39.  
    40.         }
    41.  
    42.         health = healthBarRef.GetComponent<CharacterHealth>().currentHealth;
    43.  
    44.         if(health == 0.0f) {
    45.             Debug.Log("You Have chickne");
    46.             health = 100;
    47.         }
    48.  
    49.  
    50.     }
    51. }
    52.  
    53.  
    54.  
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    I believe it is because you are moving with translate so it will ignore collision. You need to either move the rigidbody position or adjust the rigidbodies velocity. Like you are doing for your jump.

    So something like this:

    Code (csharp):
    1.  
    2.  
    3. rb.velocity = new Vector2(axisX * speed, 0);
    4.  
    5.  
    Also since you are modifying a rigid body you are going to want to use FixedUpdate and I believe Time.fixedDeltaTime. So like this:

    Code (csharp):
    1.  
    2.  
    3. void FixedUpdate()
    4. {
    5. axisX = Input.GetAxisRaw("Horizontal");
    6. bool isMovingHorizontal = Mathf.Abs(axisX) > 0.5f;
    7. float currentSpeed = speed * Time.fixedDeltaTime;
    8.  
    9. if(isMovingHorizontal)
    10. {
    11. rb.velocity = new Vector2(axisX * currentSpeed, 0);
    12. }
    13. else
    14. {
    15. rb.velocity = Vector2.zero;
    16. }
    17.  
    18. }
    19.  
    20.  
    Then you should be able to just add in the rest of your code for health and jump how you currently have it.

    I have not tested this but it should work! Hope this helps!
     
    Last edited: Aug 1, 2018