Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Character Controller - 2d topdown flipping axis?

Discussion in 'Editor & General Support' started by rbmethod, Nov 27, 2023.

  1. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    Hello,

    How can I get my character to flip the axis when facing the other direction?
     
    Last edited: Nov 27, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,292
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  3. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. [RequireComponent(typeof(Rigidbody2D))]
    7.  
    8. public class CController2D : MonoBehaviour
    9. {
    10.  
    11.     Rigidbody2D rigidbody2d;
    12.     [SerializeField] float speed = 2f;
    13.     Vector2 motionVector;
    14.     public Vector2 lastMotionVector;
    15.     Animator animator;
    16.     public bool moving;
    17.     public bool sprinting;
    18.  
    19.     void Awake()
    20.     {
    21.         rigidbody2d = GetComponent<Rigidbody2D>();
    22.         animator = GetComponent<Animator>();
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         float horizontal = Input.GetAxisRaw("Horizontal");
    28.         float vertical = Input.GetAxisRaw("Vertical");
    29.  
    30.         motionVector = new Vector2(
    31.             horizontal,
    32.             vertical
    33.             );
    34.  
    35.         animator.SetFloat("horizontal", Input.GetAxisRaw("Horizontal"));
    36.         animator.SetFloat("vertical", Input.GetAxisRaw("Vertical"));
    37.  
    38.         moving = horizontal != 0 || vertical != 0;
    39.         animator.SetBool("moving", moving);
    40.  
    41.         if (horizontal != 0 || vertical != 0)
    42.         {
    43.             lastMotionVector = new Vector2(
    44.                 horizontal,
    45.                 vertical
    46.                 ).normalized;
    47.             animator.SetFloat("lastHorizontal", horizontal);
    48.             animator.SetFloat("lastVertical", vertical);
    49.         }
    50.     }
    51.  
    52.  
    53.     void FixedUpdate()
    54.     {
    55.         Move();
    56.     }
    57.  
    58.     void Move()
    59.     {
    60.         rigidbody2d.velocity = motionVector * speed;
    61.  
    62.         if (Input.GetKey(KeyCode.LeftShift))
    63.         {
    64.             rigidbody2d.velocity = motionVector * 2;
    65.             animator.Play("L_Sprinting");
    66.         }
    67.     }
    68. }
    What do I need to do?
     
  4. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    Anyone? I'd really like to get this solved. Thank you!
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,295
  6. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,295
    Just hold onto the last horizontal input the player put in. Then, if it's less than zero, flip the x-axis (if your player is looking right by default, vice-versa otherwise).
     
  8. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    Would you mind showing me the code revision? I'm sorry I'm new...
     
  9. thekruxk

    thekruxk

    Joined:
    Aug 27, 2021
    Posts:
    1
    Btw you can also use a more simple method to flip the player.

    void Update(){
    if(horizontalnput > 0){
    gameObject.transform.localScale = new Vector3(1,1,1)
    }else if(horizontalInput<0){
    gameObject.transform.localScale = new Vector3(-1,1,1)
    }
    }


    Im sorry if it's jancky im trying to help from my phone in math class.
     
  10. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    It worked! Thank you!

    I've got the player prefab transforms set to .5x .5y so I've converted it to a Vector2, and when I move the player transforms into 1, 1, 1. How can I reset transforms on the player prefab so that this script works?