Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
float moveInputY = Input.GetAxis("Vertical");
if (moveInputY > 0)
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 newPos = Vector2.MoveTowards(rigidbody2D.position, mousePosition, Time.fixedDeltaTime * shipSpeed);
rigidbody2D.MovePosition(newPos);
}
The Sprite Renderer component has Flip, which allows you to change the side of the view, you can change it in the code itself,
private SpriteRenderer sprite;
public float speed = 3f;
private bool FacingRight = true; //задаем булевую переменную под сторону, куда будет смотреть объект
private void Awake()
{
sprite = GetComponentChildren<SpriteRender2D>();
}
private void Update()
{
float moveInput = Input.GetAxis("Horizontal"); //в Update запишем код для перемещения объекта
transform.position += new Vector3(moveInput, 0, 0) * speed * Time.deltaTime;
sprite.flipX = FacingRight < 0 ? true : false; //Задаем условие при котором будет поворачиваться объект в виде (условие - ?) (действие) (: - иначе) (действие иначе)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question