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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how to keep the direction it moved

Discussion in '2D' started by VaykorMiP, Feb 20, 2020.

  1. VaykorMiP

    VaykorMiP

    Joined:
    Feb 20, 2020
    Posts:
    7
    I'm new on unity and I'm trying to solve some problems, I created animations with sprites and I managed to alternate the various animations, I would like to know how to make sure that the character, when he is stationary, is turned to the right, if he is moved to the right, and left if it moved to the left. any help is appreciated, even a "you stupid lol"

    code: https://pastebin.com/FPkMV1kS

    demostration: https://vimeo.com/user108828555/review/392773189/f5413d068d
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @VaykorMiP

    Why not add the code here? I bet these messages will be here way longer than your paste.

    In general - get your input, but don't immediately use it.

    Store it to a variable instead and only use it, if there was actually input.

    Code (CSharp):
    1. var x = Input.GetAxis("Horizontal");
    2. var y = Input.GetAxis("Vertical");
    3. var vec = new Vector2(x,y);
    4.  
    5. if (vec.magnitude > 0)
    6. {
    7.     // set animator value
    8. }
    9.  
     
  3. VaykorMiP

    VaykorMiP

    Joined:
    Feb 20, 2020
    Posts:
    7
    sorry, i'm actually really dumb and can't understand lol, do you have any link to some video or something ? Sorry
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @VaykorMiP

    Well I didn't look into your code (as it is not here... I wish you could edit your OP).

    But something like this:
    Code (CSharp):
    1. var x = Input.GetAxisRaw("Horizontal");
    2. var y = Input.GetAxisRaw("Vertical");
    3.  
    4. var vec = new Vector2(x,y);
    5.  
    6. if (vec.x != 0)
    7. {
    8.     spriteRenderer.flipX = x < 0;
    9. }
     
    MisterSkitz likes this.
  5. VaykorMiP

    VaykorMiP

    Joined:
    Feb 20, 2020
    Posts:
    7
    the code was in the pastebin link, in any case:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BasicMovement : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.     void Update()
    10.     {
    11.  
    12.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0.0f);
    13.  
    14.         animator.SetFloat("Horizontal", movement.x );
    15.         animator.SetFloat("Vertical", movement.y);
    16.         animator.SetFloat("Magnitude", movement.magnitude);
    17.  
    18.  
    19.         transform.position = transform.position + movement * Time.deltaTime;
    20.  
    }

    }
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637