Question by
CaveiraX01 · Jan 12, 2021 at 05:18 PM ·
scripting problemscripting beginnerrotate object
Self rotate issue C#
My code to rotate a ship 180 degrees when it goes out of player range, the code is working in the bottom end but in the high end it's just tuning 5 degrees. Here is the code i'm using:
private float smooth = 5f;
public Transform player;
private float moveSpeed = -5f;
private Rigidbody2D rb2D;
void Start()
{
player = GameObject.FindWithTag("Player").transform;
if (player == null)
{
Debug.Log("Cannot find 'player' object");
}
rb2D = GetComponent<Rigidbody2D>();
rb2D.velocity = transform.up * moveSpeed;
}
void FixedUpdate()
{
Vector3 direction = player.position - transform.position;
if (direction.y < -20)
{
if (transform.rotation.z == 0f)
{
transform.Rotate(Vector3.back * 0);
}
else
{
transform.Rotate(Vector3.back * smooth);
}
rb2D.velocity = transform.up * (-1 * moveSpeed);
}
if (direction.y > 20f)
{
if (transform.rotation.z == -1f || transform.rotation.z == 1f)
{
transform.Rotate(Vector3.back * 0);
}
else
{
transform.Rotate(Vector3.back * smooth);
}
rb2D.velocity = transform.up * moveSpeed;
}
}
Comment