Search Unity

Movement player Script 2D

Discussion in '2D' started by Chemyncz, Jun 8, 2019.

  1. Chemyncz

    Chemyncz

    Joined:
    Jun 6, 2019
    Posts:
    4
    I need help to create a code to move the character, I've seen several tutorials but I'm having difficulties
     
  2. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Telling us what you're having difficulties with would help us help you. Tutorials are usually pretty good at explaining how you get simple 2d movement working so it's probably something specific blocking your progress
     
  3. Chemyncz

    Chemyncz

    Joined:
    Jun 6, 2019
    Posts:
    4
    My problem is that I did a simple script to move to the right or left and change the face of the character to the side that is being moved by pressing one of the arrows on the keyboard but when pressed to the right the character usually moves to the right , but if pressed the left changes face, but continues to move to the right instead of the left
    my code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.    public float velocidade;
    8.    
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20.         movimentacao();
    21.  
    22.         Debug.Log(transform.position);
    23.     }
    24.     void movimentacao()
    25.     {
    26.         if (Input.GetAxisRaw("Horizontal") > 0)
    27.         {
    28.             transform.Translate(Vector2.right * velocidade * Time.deltaTime);
    29.             transform.eulerAngles = new Vector2(0, 0);
    30.         }
    31.         if (Input.GetAxisRaw("Horizontal") < 0)
    32.         {
    33.             transform.Translate(-Vector2.right * velocidade * Time.deltaTime);
    34.             transform.eulerAngles = new Vector2(0, 180);
    35.         }
    36.     }
    37.  
    38. }
    39.  
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    213
    i 'think' the issue is because you're turning your object around, then also moving it reverse. when you turn your object around, your positive becomes your negative and vice versa, so you simply always translate in the same direction (always + or always -). alternatively remove the angles.
     
  5. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    As Primoz said it looks like the problem is you're turning your player around aswell as reversing the movement. Try just removing the minus before the vector2 on the moving left code
     
  6. Chemyncz

    Chemyncz

    Joined:
    Jun 6, 2019
    Posts:
    4
    And how could I become a face of my character without negative and positive investor, I have no idea how I could do this
     
  7. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Instead of rotating the player change the localScale, if you change the x scale to a negative number it will flip the sprite. You can play around with that in the inspector to get an idea how it works. Also the sprite renderer has a flip sprite function built in https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html you can use this to change the direction aswell. So there is multiple ways you can achieve this. You will just have to try them out and see which works best for you
     
unityunity