Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Flipping sprite / raycast / mouse pointer

Discussion in '2D' started by Zurvivor0412, Oct 19, 2016.

  1. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    When I move my player right and shoot it looks fine but when i go left, the sprite of the gun attached to the raycast and mouse mess up(Like in the picture). As well here is the script for the weapon.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     public float fireRate = 0;
    8.     public float Damage = 10;
    9.     public LayerMask whatToHit;
    10.     public GameObject playerObject;
    11.     private bool m_FacingRight = true;
    12.     public Transform BulletTrailPrefab;
    13.     public Transform MuzzleFlashprefab;
    14.     float timetoSpawnEffect = 0;
    15.     public float effectSpawnRate = 10;
    16.     private float move;
    17.  
    18.     float timeToFire = 0;
    19.     public Transform firePoint;
    20.  
    21.     // Use this for initialization
    22.     void Awake() {
    23.         Transform firePoint = transform.FindChild("firePoint");
    24.         if (firePoint == null) {
    25.             Debug.LogError("No FirePoint? what?!?");
    26.  
    27.         }
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update() {
    32.         if (fireRate == 0) {
    33.             if (Input.GetButtonDown("Fire1")) {
    34.                 Shoot();
    35.             }
    36.         }
    37.         else {
    38.             if (Input.GetButton("Fire1") && Time.time > timeToFire) {
    39.                 timeToFire = Time.time + 1 / fireRate;
    40.                 Shoot();
    41.                 if (move < 0)
    42.                 {
    43.                     m_FacingRight = false;
    44.                     transform.localRotation = Quaternion.Euler(0, 180, 0);
    45.                 }
    46.                 else
    47.                 {
    48.                     m_FacingRight = true;
    49.                     transform.localRotation = Quaternion.Euler(0, 0, 0);
    50.                 }
    51.             }
    52.         }
    53.     }
    54.     private void Shoot()
    55.     {
    56.         Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    57.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    58.         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, whatToHit);
    59.         if (Time.time >= timetoSpawnEffect)
    60.         {
    61.             Effect();
    62.             timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
    63.         }
    64.         Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
    65.         if (hit.collider != null)
    66.         {
    67.             Debug.DrawLine(firePointPosition, hit.point, Color.red);
    68.             Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    69.         }
    70.     }
    71.     private void Effect() {
    72.         Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
    73.         Transform Clone = Instantiate (MuzzleFlashprefab, firePoint.position, firePoint.rotation) as Transform;
    74.         Clone.parent = firePoint;
    75.         float size = UnityEngine.Random.Range(0.7f, 0.9f);
    76.         Clone.localScale = new Vector3 (size, size, size);
    77.         Destroy (Clone.gameObject, 0.2f);
    78.     }
    79. }
    80.  
     

    Attached Files:

    • Bug.png
      Bug.png
      File size:
      9.8 KB
      Views:
      754
  2. lollypap54

    lollypap54

    Joined:
    Oct 19, 2016
    Posts:
    8
    What private float move variable is doing here ?
     
  3. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    Flipping the gun the correct way (i.e. left or right) but not the way the mouse is pointing like if i turned left, the weapon will turn left but the mouse pointer and weapon fire point are opposite.