The code I use to flip the FirePoint is below in my movement script
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour
{
private float horizontal;
[SerializeField] private float speed = 8f; //the speed of the players movement
[SerializeField] private float jumpingPower = 16f; //the height of the players jump
private bool isFacingRight = true;
private bool canDash = true;
private bool isDashing;
[SerializeField]private float dashingPower = 24;
[SerializeField]private float dashingTime = 0.2f;
[SerializeField]private float dashingCooldown = 1f;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
void Update()
{
if(isDashing)
{
return;
}
horizontal = Input.GetAxisRaw("Horizontal"); //uses Unitys built in horizontal movement system
if (Input.GetButtonDown("Jump") && IsGrounded()) //detects when player is on the ground so they can jump
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) //makes it so you jump higher by holding the button down
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
if(Input.GetMouseButtonDown(1) && canDash)
{
StartCoroutine(Dash());
}
Flip();
}
private void FixedUpdate() //determines speed
{
if(isDashing)
{
return;
}
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
private bool IsGrounded() //checks where ground is
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void Flip() //flips you around if you want to walk in the other direction
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
}
Also here is what I attach to the player for his weapon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayersWeaponScript : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Shoot();
}
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
And here is what I attached to the bullet prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TheBulletScriptMovement : MonoBehaviour
{
public float speed = 20;
public Rigidbody2D rb;
void Start()
{
rb.velocity = transform.right * speed;
}
}