- Home /
Question by
MikeCodes · Apr 01, 2020 at 03:12 AM ·
rotationscripting problemvector32d spritestransform.rotation
Sprite Rotation Messed up,Rotation is choppy and messed up
I'm trying to rotate a constantly moving sprite by clicking the arrow button, it works but very badly, rotating left is extremely choppy, as if it cycles through 90 degrees and 180 degrees until it reaches 270. Also the sprite moves as it rotates making it move forward as it rotates left, which makes it look weird, any idea as to how I can solve that? Thanks!
BELOW IS CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snake_Move : MonoBehaviour
{
private Vector2 pos;
private Vector2 moveDirection;
private float moveTimer;
private float timerSeconds;
private float rotation = -90;
private void Start()
{
pos = new Vector2();
timerSeconds = 0.5f;
moveTimer = timerSeconds;
moveDirection = new Vector2(1, 0);
}
private void Update()
{
AutoMove();
ChangeDirection();
transform.position = new Vector3(pos.x, pos.y);
transform.eulerAngles = new Vector3(0, 0, rotation);
}
private void AutoMove()
{
moveTimer += Time.deltaTime;
if (moveTimer > timerSeconds)
{
pos += moveDirection;
moveTimer -= timerSeconds;
}
}
private void ChangeDirection()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (moveDirection.y != -1)
{
moveDirection.x = 0;
moveDirection.y = 1;
rotation = 0f;
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (moveDirection.y != 1)
{
moveDirection.x = 0;
moveDirection.y = -1;
rotation = 180f;
}
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (moveDirection.x != -1)
{
moveDirection.y = 0;
moveDirection.x = 1;
rotation = -90f;
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (moveDirection.x != 1)
{
moveDirection.y = 0;
moveDirection.x = -1;
rotation = 90f;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Why does this code not work? 1 Answer
Aiming to gameobject 2 Answers
Get Y Rotation Between Two Vector3 Points 2 Answers
locations being stored in map manager are inaccurate and ruining overlap validation. 2 Answers
Aligning/rotating vectors 1 Answer