Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

2d character rotation

Discussion in '2D' started by witexon, Jul 14, 2022.

?

How to rotate a character

  1. IDK

    0 vote(s)
    0.0%
  2. IDK

    0 vote(s)
    0.0%
  1. witexon

    witexon

    Joined:
    Jul 14, 2022
    Posts:
    2
    Hello everyone. I'm doing a platform game in 2d (camera on the side of the character). I made motion animations and I have a problem with how to turn the character (animations) in both directions after pressing the "A" key.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Ruch : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public float jump;
    10.     public Rigidbody2D bohater;
    11.     public float stanPrzed;
    12.     public float roznica;
    13.     private Animator animacja;
    14.     public Transform czujnik;
    15.     public float promien;
    16.     public LayerMask warstwa;
    17.     public bool dotyk;
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         stanPrzed = bohater.position.x;
    22.         dotyk = Physics2D.OverlapCircle (czujnik.position, promien, warstwa);
    23.     }
    24.  
    25.     void Start()
    26.     {
    27.         bohater = GetComponent<Rigidbody2D> ();
    28.         animacja = GetComponent<Animator> ();
    29.        
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         roznica = Mathf.Abs(stanPrzed - bohater.position.x);
    35.         animacja.SetFloat("kontrola", roznica);
    36.         if (Input.GetKey (KeyCode.D)){
    37.             bohater.velocity = new Vector2 (speed, bohater.velocity.y);
    38.         }
    39.         if (Input.GetKey (KeyCode.A)){
    40.             bohater.velocity = new Vector2 (-speed, bohater.velocity.y);
    41.            
    42.         }
    43.         if (Input.GetKey (KeyCode.Space) && dotyk){
    44.             bohater.velocity = new Vector2 (bohater.velocity.x, jump);
    45.         }
    46.        
    47.            
    48.        
    49.     }
    50. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,102
    "I have a problem" is not useful.

    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, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.