Answer the question
In order to leave comments, you need to log in
How to remove inversion when tracking the cursor?
I have a character in the game, a weapon.
When the character looks to the right, then the weapon is normal behind him,
but when the character flips to the left, the weapon takes on an inversion
, that is, the cursor is on the top right, and the weapon is on the bottom left
https://youtu.be/ x2lAk6f3sW0
Answer the question
In order to leave comments, you need to log in
GGWPKATASI , I'll explain again. When you rotate a character, you only rotate the character. The weapon sprite is still rotated where it was rotated. You need the weapon to flip too. Now I will show an example.
There is a player script.
using UnityEngine;
namespace Assets.Scripts.Rotation
{
[RequireComponent(typeof(Rigidbody2D))] // Нужен, чтобы компонент Rigidbody2D всегда был на объекте, к которому прикреплен данный скрипт
public class Player : MonoBehaviour
{
[SerializeField] private float _maxSpeed; //[SerializeField] позволяет видеть private поля в инспекторе
[SerializeField] private float _speed;
[Range(0f, 1f)]
[SerializeField] private float _slowDownSpeed; //Range ограничивает между 0 и 1 в данном случае
[SerializeField] private Gun _gun;
private Vector2 _movementDirection;
private Rigidbody2D _rb;
private void Start()
{
_rb = GetComponent<Rigidbody2D>(); // Получаем компонент, если получаете компонент таким образом, обязательно нужно делать [RequireComponent(typeof(НазваниеКомпонента))]
}
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
_movementDirection = new Vector2(horizontal, vertical);
}
private void FixedUpdate() // все действия с физикой в FixedUpdate
{
if (_movementDirection == Vector2.zero) SlowDown(_slowDownSpeed);
Move(_movementDirection, _speed);
Flip();
}
private void Flip()
{
var vector = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //берем вектор направления от нашей позиции до позиции мыши в мировых координатах
if (vector.x < 0) transform.eulerAngles = new Vector3(0, 180); // Если он левее, то поворачиваем объект налево
if (vector.x >= 0) transform.eulerAngles = new Vector2(0, 0);// или направо, если он правее
_gun.Flip(vector.x);// поворачиваем оружие
}
private void Move(Vector2 direction, float speed)
{
var velocity = _rb.velocity;
var x = velocity.x + direction.x * speed;// определяем новую скорость
var y = velocity.y + direction.y * speed;
velocity.x = Mathf.Clamp(x, -_maxSpeed, _maxSpeed); //ограничиваем скорость
velocity.y = Mathf.Clamp(y, -_maxSpeed, _maxSpeed);
_rb.velocity = velocity; // присваиваем скорость
}
private void SlowDown(float speed) // замедляем объект
{
var velocity = _rb.velocity;
velocity.x = Mathf.Lerp(velocity.x, 0, speed);
velocity.y = Mathf.Lerp(_rb.velocity.y, 0, speed);
_rb.velocity = velocity;
}
}
}
using UnityEngine;
namespace Assets.Scripts.Rotation
{
[RequireComponent(typeof(SpriteRenderer))]
public class Gun : MonoBehaviour
{
private SpriteRenderer _spriteRenderer;
private void Start()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
}
private void Update()
{
var difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
var rotation = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotation); // ваш код поворота оружия
}
public void Flip(float direction)
{
_spriteRenderer.flipY = direction < 0; // отражаем спрайт по оси Y в зависимости от того, куда смотрит персонаж
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question