Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to change the sprite when jumping, going left or right? (I'm new to unity)

Discussion in 'Scripting' started by NotD4NI, Dec 5, 2020.

  1. NotD4NI

    NotD4NI

    Joined:
    Dec 5, 2020
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController2D controller;
    8.    
    9.     public float moveSpeed = 5f;
    10.    
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.    
    17.     void Update()
    18.    
    19. {
    20.         Jump();
    21.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    22.         transform.position += movement * Time.deltaTime * moveSpeed;
    23.  
    24.        
    25.     }
    26.  
    27.     void Jump()
    28.     {
    29. if (Input.GetButtonDown("Jump"))
    30. {
    31. gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 10f), ForceMode2D.Impulse);
    32.  
    33. }
    34.  
    35.     }
    36. }
    37.  
     

    Attached Files:

  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    There is just about a gazillion tutorials on how to get the basics of a 2D game working.
    I suggesting looking for one on youtube.
     
  3. NotD4NI

    NotD4NI

    Joined:
    Dec 5, 2020
    Posts:
    2
    The thing is they are all outdated (atleast the ones i found), and the code or stuff they're showing doesnt work anymore. If you could, could you link me a video that works?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    What about them does not work? Just as a super quick answer, to change the direction your sprite looks in, you can just flip its scale on the corresponding axis (ie multiply by -1). Walking, jumping and other movements are animations you play while certain actions are executed or certain criteria are met.
    I'm not too familiar with 2D, but to my knowledge not a whole lot about any of this changed lately - at least not to a degree that would make it impossible to follow a tutorial by doing the same things.