- Home /
Question by
ReubenClare123 · Jul 31, 2019 at 10:53 PM ·
movementshootingflip
why wont my fire point flip?
Hi guys, im a huge noob and need help. I can shoot in the RIGHT direction fine but when I flip to the LEFT the fireposition doesn't flip and I just shoot myself in the back. Need help, these are my scripts
Player flip script :
if (Input.GetKeyDown(KeyCode.D))
{
mySpriteRenderer.flipX = false;
}
else if (Input.GetKeyDown(KeyCode.A))
{
mySpriteRenderer.flipX = true;
}
}
}
This is the shoot script:
public class ShootDemo : MonoBehaviour { [SerializeField] GameObject projectilePrefab;
// Shoot needs to know the direction, so we just ask the renderer if we are flipped
// Link the player sprite renderer to this in inspector
[SerializeField] public SpriteRenderer mySpriteRenderer;
private void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
GameObject go = Instantiate(projectilePrefab, transform.position, transform.rotation);
Rigidbody2D rb2d = go.GetComponent<Rigidbody2D>();
// Lets get our direction from renderer
int facing = mySpriteRenderer.flipX == true ? -1 : 1;
rb2d.velocity = transform.TransformDirection(new Vector3(20f, 0, 5f)) * facing;
}
}
}
This is the move script:
// Use this for initialization void Start()
{
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
void Update()
{
Vector2 move = Vector2.zero;
float horizontal = Input.GetAxis("Horizontal"); // a or left = -1 d or right = 1
rb.velocity = new Vector2(horizontal * Speed, rb.velocity.y);
animator.SetFloat("Speed", Mathf.Abs(horizontal));
if (Input.GetButtonDown("Jump") && grounded)
{
velocity.y = jumpTakeOffSpeed;
}
else if (Input.GetButtonUp("Jump"))
{
if (velocity.y > 0)
{
velocity.y = velocity.y * 0.5f;
}
}
animator.SetBool("grounded", grounded);
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
targetVelocity = move * maxSpeed;
}
}
Thanks guys!
Comment