On Player Flip change shooting direction?

Updated on May 5, 2017 in [A] 2D
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
0 on May 5, 2017

Hello everyone I’ve been looking everywhere online to fix my problem but couldn’t find what i was looking for, wondering if someone could help me out here, Thanks! Basically I have a player with normal controls left,right & jump. I have a hand that is completely separated from the player body. the hand has a pivot to the body to stay in place on the player. When the player flips the hand flips with the player to the direction player facing. I have followed Brackeys tutorial to create 360 hand rotation and shooting.

When my player is flipped right with the hand the bullets go to the direction where the firePoint is pointing at, (360 degrees.)

But my problem is when player is flipped left with the hand the bullets continue going to the same direction like the player is still flipped to the right.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCharacter : MonoBehaviour {

//movement varibles

Rigidbody2D rb;
bool facingRight = true;

[SerializeField] float maxSpeed = 10f;
[SerializeField] float jumpForce = 400f;

[Range(0, 1)]
[SerializeField] float crouchSpeed = .36f;

[SerializeField] bool airControl = false;
[SerializeField] LayerMask whatIsGround;

Transform groundCheck;
float groundedRadius = .02f;
bool grounded = false;
Transform ceilingCheck;
float ceilingRadius = .01f;
Animator anim;

Transform playerGraphics; //reference to the graphics so we can change direction.

//reference to the graphics so we can change direction

void Start()
{
rb = GetComponent<Rigidbody2D>();
}

void Awake()
{
//setting up references.
groundCheck = transform.Find(“GroundCheck”);
ceilingCheck = transform.Find (“CeilingCheck”);
anim = GetComponent<Animator> ();
playerGraphics = transform.FindChild (“Graphics”);
if (playerGraphics == null) {
Debug.LogError (“Lets freak out! there is no ‘Graphics’ object as a child of a player”);
}
}

void FixedUpdate()
{
//The player is grounded if a circlecast to the groundcheck position hits anything disignated as ground
grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
anim.SetBool (“Ground”, grounded);

//Set the vertical animation
anim.SetFloat(“vSpeed”, rb.velocity.y);
}

public void Move(float move, bool crouch, bool jump)
{
//if crouching check if the character can stand up, keep them crouching
if (!crouch && anim.GetBool (“Crouch”)) {
if (Physics2D.OverlapCircle (ceilingCheck.position, ceilingRadius, whatIsGround))
crouch = true;
}

//Set whether or not the character is crouching in the animator
anim.SetBool (“Crouch”, crouch);

//only control the player if grounded or airControl is turned on
if (grounded || airControl) {
//reduce the speed if crouching by the crouchSpeed multiplier
move = (crouch ? move * crouchSpeed : move);

//the speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat (“Speed”, Mathf.Abs (move));

//Move the character
rb.velocity = new Vector2 (move * maxSpeed, rb.velocity.y);

//If the input is moving the player right and the player is facing left…
if (move > 0 && facingRight)
//… flip the player.
Flip ();
//Otherwise if the input is moving the player left and the player is facing right…
else if (move < 0 && !facingRight)
//…flip the player.
Flip ();
}

// if the player should jump…
if (grounded && jump) {
//Add a vertical force to the player.
anim.SetBool (“Ground”, false);
rb.AddForce (new Vector2 (0f, jumpForce));
}
}

void Flip ()
{
//Switch the way the player is labelled as facing.
facingRight = !facingRight;

//Multiply the player’s x local scale by -1.
Vector3 theScale = playerGraphics.localScale;
theScale.x *= -1;
playerGraphics.localScale = theScale;
}

}

using UnityEngine;

namespace UnitySampleAssets2D
{

[RequireComponent(typeof (PlayerCharacter))]
public class playerControlKeys : MonoBehaviour
{
private PlayerCharacter character;
private bool jump;

private void Awake()
{
character = GetComponent<PlayerCharacter>();
}

private void Update()
{
if(!jump)
// Read the jump input in Update so button presses aren’t missed.
if (Input.GetButtonDown(“Jump”)) jump = true;
}

private void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftShift);
float h = Input.GetAxis(“Horizontal”);
// Pass all parameters to the character control script.
character.Move(h, crouch, jump);
jump = false;
}
}

}

using UnityEngine;
using System.Collections;

public class ArmRotation : MonoBehaviour {

public int rotationOffset = 90;

// Update is called once per frame
void Update () {
// subtracting the position of the player from the mouse position
Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) – transform.position;
difference.Normalize (); // normalizing the vector. Meaning that all the sum of the vector will be equal to 1

float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg; // find the angle in degrees
transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
}
}

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

public float fireRate = 0;
public float Damage = 10;
public LayerMask whatToHit;

public Transform BulletTrailPrefab;
public Transform MuzzleFlashPrefab;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 10;

float timeToFire = 0;
Transform firePoint;

// Use this for initialization
void Awake () {
firePoint = transform.FindChild (“FirePoint”);
if (firePoint == null) {
Debug.LogError (“No firePoint? WHAT?!”);
}
}

// Update is called once per frame
void Update () {
if (fireRate == 0) {
if (Input.GetButtonDown (“Fire1”)) {
Shoot();
}
}
else {
if (Input.GetButton (“Fire1”) && Time.time > timeToFire) {
timeToFire = Time.time + 1/fireRate;
Shoot();
}
}
}

void Shoot () {
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
if (Time.time >= timeToSpawnEffect) {
Effect ();
timeToSpawnEffect = Time.time + 1/effectSpawnRate;
}
Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
Debug.Log (“We hit ” + hit.collider.name + ” and did ” + Damage + ” damage.”);
}
}

void Effect () {
Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
clone.parent = firePoint;
float size = Random.Range (0.6f, 0.9f);
clone.localScale = new Vector3 (size, size, size);
Destroy (clone.gameObject, 0.02f);
}
}

Any help would be really appreciated! Thank you for your time!

  • Liked by
Reply