B
B
BrightOnX2020-05-14 17:53:22
Unity
BrightOnX, 2020-05-14 17:53:22

Why does an object rotate incorrectly in Unity2d?

I'm trying to make a rotation of the hand of the character. Joystick movement, android game.
Those. If the joystick is pointing to the left, then both the character and the hand look to the left, the same is true to the right.
But my hand and character normally look only to the right, but if you turn the character to the right, then it will turn normally, but the hand will continue to look to the right, I could not find an answer to this, and if I did it myself, then the hand still does not wanted to rotate normally
Here are the pictures:
5ebd5ab5a59e3357349893.jpeg5ebd5abb0a7f2424553259.jpeg

Here are the scripts:
PlayerMovement.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PlayerMovement : MonoBehaviour
{
    public Joystick joys;// джойстик
    public float speed; //скорость
    Rigidbody2D rb;
    public bool is_right=true;
 
    void Start()
    {
        rb = GetComponent<Rigidbody2D>(); //подключаем rigidbody
    }

    void Update()
    {

        if (is_right && joys.Horizontal < 0) //если игрок повернуть вправо и джойстик повернут вправо, то перевернуть
        {
            
                Flip();

        }
        else if (!is_right && joys.Horizontal > 0) // тоже самое, только в обратную сторону
        {
            
                Flip();
        }
                    
       
    }

    void FixedUpdate() {

        
        rb.velocity = new Vector2(joys.Horizontal * speed,joys.Vertical*speed); // движение игрока
            
    }

   public void Flip() // переворот
    {
        is_right = !is_right;
        Vector3 movex = transform.localScale;
        movex.x *= -1;
        transform.localScale = movex;
    }

}


hanRotation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;


public class hanRotation : MonoBehaviour
{
   

    PlayerMovement player; 
    public Joystick joyshoot; //тот же джойстик, который управляет игроком
    public GameObject myPlayer; //игрок
    
   

    private void Start()
    {
        player = GameObject.Find("Player").GetComponent<PlayerMovement>();
    }
    private void FixedUpdate()
    {
        float vertical = joyshoot.Vertical;
        float horizontal = joyshoot.Horizontal;

        float rotZ = Mathf.Atan2(vertical, horizontal) * Mathf.Rad2Deg;
       
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ);    //вращение рукой

            if (rotZ < -90 || rotZ > 90)
        {
            if (myPlayer.transform.eulerAngles.y == 0)
            {

                transform.localRotation = Quaternion.Euler(180, 0, -rotZ);

            }
            else if (myPlayer.transform.eulerAngles.y == 180)
            {

                transform.localRotation = Quaternion.Euler(180, 180, -rotZ);

            }
            }
        }       

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Prizm, 2020-05-14
@BrightOnX

Perhaps the error is in these lines:
transform.localRotation = Quaternion.Euler(180, 0, -rotZ);
transform.localRotation = Quaternion.Euler(180, 180, -rotZ);
the fact is that the body is reflected in the line
transform.localScale = movex;
because of what, the hands are reflected a second time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question