Answer the question
In order to leave comments, you need to log in
Why does an object disappear during rotation?
I am making code for the game, it is necessary that the character can move from point to point and turn in the direction of movement. The movement works, but when I try to rotate my 2d object, it doesn't rotate, it just disappears. I understand that this is due to the fact that, in addition to z, both x and y become 90 degrees and my 2d object seems to rise with an edge and therefore it is not visible, but how to make it normal
Script
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ZombieStandart : MonoBehaviour
{
public float speed = 1f;
public int health;
public int atack;
public Transform[] WayPoints;
public int NewPosition;
public Transform LastPosition;
public void Start()
{
NewPosition = 0;
}
public void FixedUpdate()
{
Move();
Rotation();
}
public void Move()
{
transform.position = Vector2.MoveTowards(transform.position, WayPoints[NewPosition].position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, WayPoints[NewPosition].position) < 0.2f)
{
NewPosition = NewPosition + 1;
}
}
public void Rotation() // вот тут пытаюсь повернуть но что-то не хочет
{
Vector2 dir = WayPoints[NewPosition].position - transform.position;
Quaternion rotation = Quaternion.LookRotation(dir, Vector2.zero);
transform.rotation = rotation;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
}
}
Answer the question
In order to leave comments, you need to log in
If you need to stupidly rotate the character in the opposite direction from the movement, isn't it easier to change the Flip bool variable in the Sprite Renderer component? Or you can do this:
void Flip()
{
if (Input.GetAxis("Horizontal") < 0) transform.localRotation = Quaternion.Euler(0, 0, 0);
if (Input.GetAxis("Horizontal") > 0) transform.localRotation = Quaternion.Euler(0, 180, 0);
}
You can make the object a child of the camera, then it will work the way you want.
By the way, the camera sees objects at a distance from and to, that is, the object may be too close.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question