Hi, I have this script attached to a 2D sprite that is meant to take rotate to a different position based on which arrow theu ser presses. But in the vertical section, instead of rotating it to the direction the code specifies, it just set the sprite’s z rotation to 180. What did I do wrong?
function ChooseShotDirection(){
if(Input.GetAxis("Fire Horizontal") > 0){
transform.rotation.z = 0;
transform.rotation.y = 180;
Debug.Log("Shot Right");
}
else if(Input.GetAxis("Fire Horizontal") < 0){
transform.rotation.z = 0;
transform.rotation.y = 0;
Debug.Log("Shot left");
}
else if(Input.GetAxis("Fire Vertical") > 0){
transform.rotation.z = 270;
transform.rotation.x = 0;
transform.rotation.y = 0;
Debug.Log("Shot Up");
}
else if(Input.GetAxis("Fire Vertical") < 0){
transform.rotation.z = 90;
transform.rotation.x = 0;
transform.rotation.y = 0;
Debug.Log("Shot Down");
}
}
not sure what the problem is, but I don’t think you should be able to set the rotations correctly like that if you are using c#. Try this instead of setting the values separately.
transform.eulerAngles = new Vector3(xValue, yValue, zValue);
or
transform.rotation = Quaternion.Euler(new Vector3(xValue, yValue, zValue));
I manged to fix the issue like this. I set the sprite to its up position by default and saved it in the UpPosition var. Then for the horizontal rotations I did it conventional, but the vertical ones I referred to the UpPosition var and inverted it for down.
function ChooseShotDirection(){
if(Input.GetAxis("Fire Horizontal") > 0){
transform.rotation.z = 0;
transform.rotation.y = 180;
transform.rotation.x = 0;
var TheShotR = Instantiate(Ammo, Right.position, Quaternion.identity);
TheShotR.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 270);
//Debug.Log("Shot Right");
}
else if(Input.GetAxis("Fire Horizontal") < 0){
transform.rotation.z = 0;
transform.rotation.y = 0;
transform.rotation.x = 0;
var TheShotL = Instantiate(Ammo, Left.position, Quaternion.identity);
TheShotL.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 90);
//Debug.Log("Shot left");
}
else if(Input.GetAxis("Fire Vertical") > 0){
transform.rotation = UpPosition;
transform.rotation.x = 0;
var TheShotU = Instantiate(Ammo, Up.position, Quaternion.identity);
//TheShotU.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 180);
//Debug.Log("Shot Up");
}
else if(Input.GetAxis("Fire Vertical") < 0){
transform.rotation = UpPosition;
transform.Rotate(180,180,0);
var TheShotD = Instantiate(Ammo, Down.position, Quaternion.identity);
TheShotD.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 180);
//Debug.Log("Shot Down");
}
}