I have a 2D sprite that looks like a meteor. I’m trying to get it to rotate on the x-axis. When I slow down the speed of the sprite so I can look at the rotation it doesn’t visually look like the sprite is rotating even though it says it is in the inspector. Why is this? Is there something about 2D sprites that I need to understand?
Also, the sprite is moving on the z-axis. How do I constrain this? I’ve tried constraining the z-axis rotation on the object. My code for the rotation specifically is ‘transform.Rotate (Vector3.right, speed * Time.deltaTime, Space.Self);’ (which is two lines below void Update).
I’m showing you all of my code because it may help you as a reference.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
[SerializeField]
float speed;
float radius;
Vector2 direction;
// Use this for initialization
void Start () {
direction = Vector2.one.normalized; // direction is (1, 1) normalized
radius = transform.localScale.x / 2; // half the width
}
// Update is called once per frame
void Update () {
transform.Translate(direction * speed * Time.deltaTime);
**transform.Rotate(Vector3.right, speed * Time.deltaTime, Space.Self);** // Rotate on x-axis
// Bounce off top and bottom
if (transform.position.y < GameManager.bottomLeft.y + radius && direction.y < 0)
{
direction.y = -direction.y;
}
if (transform.position.y > GameManager.topRight.y - radius && direction.y > 0)
{
direction.y = -direction.y;
}
// Game over
//if (transform.position.x < GameManager.bottomLeft.x + radius && direction.x < 0)
// {
// Debug.Log("Right player wins!!");
// // For now, just freeze time
// Time.timeScale = 0;
// enabled = false; // Stop updating script
// }
if (transform.position.x > GameManager.topRight.x - radius && direction.x > 0)
{
Debug.Log("Left player wins!!");
// For now, just freeze time
Time.timeScale = 0;
enabled = false; // Stop updating script
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Paddle")
{
bool isRight = other.GetComponent<Paddle>().isRight;
// If hitting right paddle and moving right, flip direction
if (isRight == true && direction.x > 0) {
direction.x = -direction.x;
}
// If hitting left paddle and moving left, flip direction
if (isRight == false && direction.x < 0)
{
direction.x = -direction.x;
}
}
if (other.tag == "Wall")
{
direction.x = -direction.x;
Debug.Log("You've hit the wall");
}
}
}