Issue
This Content is from Stack Overflow. Question asked by Brent Sheldon
Below is the code I’m using. Basically, it is a ball with the sprite of a tiny
spaceship that rotates around a ring. It has a trail renderer attached and
when I change the direction the trail comes out the top in the wrong direction.
I need to know how to flip the ship to match the trail.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
}
}
Solution
Hard to pinpoint ur error without knowing further details.
Is your "ball" a 3D Sphere or a 2D Circle?
What component is used to display your spaceship image?
if its the SpriteRenderer, you can get a reference to that and flip the sprite
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
SpriteRenderer m_SpriteRenderer;
void Start()
{
//Fetch the SpriteRenderer from the GameObject
m_SpriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
//flip sprite along the y axis
m_SpriteRenderer.flipY = !m_SpriteRenderer.flipY
}
}
This Question was asked in StackOverflow by Brent Sheldon and Answered by Nico Rathje It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.