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

Flip The Sprite By Using Touchpad

Discussion in 'Scripting' started by Pixellate32, Apr 2, 2020.

  1. Pixellate32

    Pixellate32

    Joined:
    Mar 22, 2020
    Posts:
    6
    Hi, I am creating 2D RPG game for mobile.I am using touchpad to control the character.I want to flip the character when he is going backward.I know how to do it with character controller but I am not using that.So what should I do to flip the player when moving backward.
    Code (CSharp):
    1. public Transform player;
    2.     public float speed = 0f;
    3.     private bool touchStart = false;
    4.     private Vector2 pointA;
    5.     private Vector2 pointB;
    6.  
    7.     public Transform circle;
    8.     public Transform outerCircle;
    9.  
    10.     public Animator anim;
    11.  
    12.     void Start (){
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         //Player Controller
    19.         if(Input.GetMouseButtonDown(0)){
    20.             speed = 3;
    21.             pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    22.             circle.transform.position = pointA * 1;
    23.             outerCircle.transform.position = pointA * 1;
    24.             circle.GetComponent<SpriteRenderer>().enabled = true;
    25.             outerCircle.GetComponent<SpriteRenderer>().enabled = true;
    26.         }
    27.         if(Input.GetMouseButton(0)){
    28.             speed = 3;
    29.             touchStart = true;
    30.             pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    31.         }else{
    32.             speed = 0;
    33.             touchStart = false;
    34.         }
    35.  
    36.         //Player Animation
    37.         anim.SetFloat ("Move", speed);
    38.  
    39.     }
    40.     private void FixedUpdate(){
    41.         if (touchStart) {
    42.             Vector2 offset = pointB - pointA;
    43.             Vector2 direction = Vector2.ClampMagnitude (offset, 1.0f);
    44.             moveCharacter (direction * 1);
    45.  
    46.             circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * 1;
    47.         }else{
    48.             circle.GetComponent<SpriteRenderer>().enabled = false;
    49.             outerCircle.GetComponent<SpriteRenderer>().enabled = false;
    50.         }
    51.  
    52.         }
    53.        
    54.     void moveCharacter(Vector2 direction){
    55.         player.Translate(direction * speed * Time.deltaTime);
    56.     }
    57. }
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    In FixedUpdate you are calculating the direction so if direction.x > 0 then face right, if direction.x < 0 then face left.

    Some advice:
    1. Multiplying any number by 1 is redundant. E.g direction * 1 effectively does nothing but waste cpu cycles.
    2. GetComponent is not a cheap function, cache those values in Awake or Start and reuse the reference instead of calling it in Update or FixedUpdate.
    3. If you are not using physics there is no real need to use FixedUpdate, use Update. It doesn't really help with performance if that's what you are after. It can be called multiple times each frame as it attempts to have a consistent amount of calls over time making it have more impact on performance.
    4. Keep it up, you only get better with practice.
     
    Pixellate32 likes this.
  3. Pixellate32

    Pixellate32

    Joined:
    Mar 22, 2020
    Posts:
    6
    Thanks for the help.It helped me alot