Translating a transform also rotates it (Unity 2017.3.1f1)
I'm building a little scene in 2D. My player game object is a sphere sprite and to it is attached the script that you can see below. The player can shoot projectiles which are visualized as a stretched sprite.
For now, I only want to place the projectile in correct orientation above, right, below, and left of the player game object. Because the projectile sprite has a horizontal orientation by default, I'm only modifying the rotation for the UpArrow and DownArrow keys.
Both when I only rotate the projectile transform (left picture below) and when I only translate it (right picture below), it works as expected. (On the left I selected the correctly oriented projectile sprite because it wouldn't be visible otherwise)
But when I rotate and then translate the projectile transform, the translation does not move it where it should but in the angle that I supplied for the rotation (intended position in red)
This script is attached to my player game object:
public class Shoot : MonoBehaviour {
public GameObject projectilePrefab;
GameObject projectile = null;
private void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow)){
projectile = Instantiate(projectilePrefab);
projectile.transform.Translate(Vector2.right);
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
projectile = Instantiate(projectilePrefab);
projectile.transform.Translate(Vector2.left);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
projectile = Instantiate(projectilePrefab);
projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
projectile.transform.Translate(Vector2.up);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
projectile = Instantiate(projectilePrefab);
projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
projectile.transform.Translate(Vector2.down);
}
}
}
Answer by mlaveg · Jul 08, 2018 at 12:13 PM
I hadn't been aware that transform.Translate() by default translates along the transform's own Vector2.up which is modified by
projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
Writing
projectile.transform.Translate(Vector2.up, Space.World);
and
projectile.transform.Translate(Vector2.down, Space.World);
respectively solves it.
Your answer
Follow this Question
Related Questions
Can Unity make this kind of scale/transform? 1 Answer
Animation changes constantly reset themselves. 1 Answer
Rotate y-axis of child object following a rigidbody sphere parent 0 Answers
How to use Quaternion.Slerp with transform.LookAt? 3 Answers
Return Object to Original Position after it's Disabled 1 Answer