Question by
mirzoboev · Nov 24, 2020 at 07:03 PM ·
gameobjecttransformshooting
How to change X and Y axis of the gameObject while moving?
I want to move an object in 4 directions (up, down, left, right) and want to change its X and Y axis. My gameObject has a child object gun (highlighted green), where bullets are spawning. In short, gameObject should turn left when I click the left button and shoot that way. But it is not working with the script I wrote. Would be great if somebody helps me.
public class Player : MonoBehaviour
{
public Gun gun;
public float verticalInput = 0;
public float horizontalInput = 0;
private bool facingTop = true;
private void Update()
{
if (Input.GetKeyDown("space"))
{
gun.Shoot();
}
}
private void FixedUpdate()
{
verticalInput = Input.GetAxis("Vertical");
horizontalInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalInput * 5f, verticalInput * 5f);
if (!facingTop && verticalInput > 0)
{
FlipVertical();
} else if(facingTop && verticalInput < 0)
{
FlipVertical();
} else if(facingTop && horizontalInput > 0 )
{
FlipRight();
} else if(facingTop && horizontalInput < 0)
{
FlipLeft();
}
}
private void FlipVertical()
{
facingTop = !facingTop;
transform.Rotate(0, 0, 180);
}
private void FlipRight()
{
facingTop = !facingTop;
transform.Rotate(0, 0, 270);
}
private void FlipLeft()
{
facingTop = !facingTop;
transform.Rotate(0, 0, 90);
}
}
screenshot-2020-11-23-184515.jpg
(6.5 kB)
Comment