Search Unity

My character can't jump/Mi personaje no puede saltar

Discussion in '2D' started by andresbe1205, Jun 24, 2021.

  1. andresbe1205

    andresbe1205

    Joined:
    Jun 24, 2021
    Posts:
    2
    When I press "w" my character don't jump, I tagged the platform and I used the "private void OnCollisionEnter2D(Collision2D collision)", this is the script:/ Cuando presiono "w" my pesonaje no salta, yo "taggie" la plataforma y use "private void OnCollisionEnter2D(Collision2D collision)" este es el codigo:



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovimientoI : MonoBehaviour
    6. {
    7.     bool canJump = true;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         //Izquierda
    18.         if (Input.GetKey("a"))
    19.         {
    20.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-200f * Time.deltaTime, 0));
    21.         }
    22.         // Izquierda, más veloz
    23.         if (Input.GetKey("a") && Input.GetKey(KeyCode.RightShift))
    24.         {
    25.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-600f * Time.deltaTime, 0));
    26.         }
    27.         // Derecha
    28.         if (Input.GetKey("d"))
    29.         {
    30.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(200f * Time.deltaTime, 0));
    31.         }
    32.         // Derecha, más veloz
    33.         if (Input.GetKey("d") && Input.GetKey(KeyCode.RightShift))
    34.         {
    35.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(600f * Time.deltaTime, 0));
    36.         }
    37.         if (Input.GetKeyDown("w") && canJump)
    38.         {
    39.             canJump = false;
    40.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 200f));
    41.         }
    42.     }
    43.     private void OnCollisionEnter2D(Collision2D collision)
    44.     {
    45.         if (collision.transform.tag == "platform")
    46.         {
    47.             canJump = true;
    48.         }
    49.     }
    50. }
     
  2. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    108
    Y-hello there,

    the culprit is in the OnCollisionEnter2D, the right call is
    collision.gameObject.tag == "platform"
    not transform.

    Also for future reference, calling GetComponent each time is a waste of resources, instead you can create a Rigidbody2D variable and referencing it in the Awake() or Start() methods like:

    Code (CSharp):
    1. Rigidbody2D myBody;
    2. void Awake()
    3. {
    4.    if(myBody == null)
    5.       myBody = gameObject.GetComponent<Rigidbody2D>();
    6. }  
    and then you can just call:


    myBody.AddForce(new Vector2(-200f * Time.deltaTime, 0));


    Hope this helps :)
     
    andresbe1205 likes this.
  3. andresbe1205

    andresbe1205

    Joined:
    Jun 24, 2021
    Posts:
    2
    Thank you, I had not payed attention to that mistake.
     
  4. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    108
    No problem :D

    Weirdly enough Unity does not throw an error if you use transform.tag