Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2d shooter, shooting backwards

Discussion in '2D' started by theguywhocodes, Feb 4, 2015.

  1. theguywhocodes

    theguywhocodes

    Joined:
    Jan 3, 2015
    Posts:
    3
    I was following brackeys tutorial about shooting in his 2d platformer video. I made my own arm rotation script, but i am using his weapons script. For some reason the shooting is flipped, done research to no avail

    weapon script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Weapon : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public float Damage = 10;
    8.     public LayerMask whatToHit;
    9.  
    10.     float timeToFire = 0;
    11.     Transform firePoint;
    12.  
    13.     // Use this for initialization
    14.     void Awake () {
    15.         firePoint = transform.FindChild ("FirePoint");
    16.         if (firePoint == null) {
    17.             Debug.LogError ("No firePoint? WHAT?!");
    18.         }
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.         if (fireRate == 0) {
    24.             if (Input.GetButtonDown ("Fire1")) {
    25.                 Shoot();
    26.             }
    27.         }
    28.         else {
    29.             if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    30.                 timeToFire = Time.time + 1/fireRate;
    31.                 Shoot();
    32.             }
    33.         }
    34.     }
    35.  
    36.     void Shoot () {
    37.         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    38.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    39.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
    40.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    41.         if (hit.collider != null) {
    42.             Debug.DrawLine (firePointPosition, hit.point, Color.red);
    43.             Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    44.         }
    45.     }
    46. }
    47.  
    48.  
    armrotation script:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var rotationOffset : int = 0;
    5.  
    6. function Update ()
    7. {
    8.  
    9.     var object_pos : Vector3 = transform.position;
    10.  
    11.     var mouse_pos : Vector3 = Input.mousePosition;
    12.  
    13.     object_pos = Camera.main.WorldToScreenPoint(transform.position);
    14.  
    15.     mouse_pos.x = mouse_pos.x - object_pos.x;
    16.     mouse_pos.y = mouse_pos.y - object_pos.y;
    17.  
    18.     var rotZ = Mathf.Atan2(mouse_pos.y, mouse_pos.x) *    Mathf.Rad2Deg;
    19.  
    20.     transform.rotation = Quaternion.Euler(Vector3(00, 0, rotZ + rotationOffset));
    21. }
    22.  
    Any help would be appreciated :)
     
  2. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    654
    That arm rotation script is extremely inefficient......
    use this instead:
    Code (csharp):
    1.  
    2.         //Get the position of our transform on screen space
    3.         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
    4.         //Get the direction of the mouse from the player
    5.         Vector3 dir = Input.mousePosition - pos;
    6.         //Get the angle at which the mouse it relative to the player
    7.         float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
    8.  
    9.         //Set the rotation to the angle of the mouse position relative to the player
    10.         transform.rotation = Quaternion.Euler(0f, 0f, angle);
    11.  

    Im not going to convert it to javascript because I dont have the time, but it should be very easy for you. :)
     
  3. theguywhocodes

    theguywhocodes

    Joined:
    Jan 3, 2015
    Posts:
    3
    Works great, still got the same problem while shooting :(
     
  4. GamesRocketGas

    GamesRocketGas

    Joined:
    Feb 4, 2015
    Posts:
    2
    i have got a problem with that code:



    usingUnityEngine;
    usingSystem.Collections;

    publicclassshooting : MonoBehaviour {
    privateTouchinitialTouch = newTouch();
    privatefloatdistance = 0;
    privateboolswipedleft = false;
    privateboolswipedright = false;
    privateTransformsightStart, sightMiddle, sightMiddle1, sightEnd;
    privateTransformsightStart2, sightMiddle2, sightMiddle3, sightEnd2;


    voidUpdate()
    {
    Raycasting ();
    }

    voidFixedUpdate()
    {{
    foreach(TouchtinInput.touches)
    {
    if (t.phase == TouchPhase.Began)
    {
    initialTouch = t;
    }
    elseif (t.phase == TouchPhase.Moved)
    {
    floatdeltaX = initialTouch.position.x - t.position.x;
    floatdeltaY = initialTouch.position.y - t.position.y;
    distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
    boolswipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);

    if (distance > 100f)
    {
    if (swipedSideways && deltaX > 0)//shootleft
    {
    swipedleft = true;
    }
    elseif (swipedSideways && deltaX <= 0)//shootright
    {
    swipedright = true;
    }

    }}
    elseif (t.phase == TouchPhase.Ended)
    {
    initialTouch = newTouch();
    }}}}

    voidRaycasting (){

    if (swipedleft == true) {
    swipedleft = false;
    Debug.DrawLine (sightStart.position, sightMiddle.position, sightMiddle1.position, sightEnd.position, Color.green);
    }


    if(swipedright == true) {
    swipedright = false;
    Debug.DrawLine(sightStart2.position, sightMiddle2.position, sightMiddle3.position, sightEnd2.position, Color.green);}

    }

    }


    it says you have to use UnityEngine.Vector 3 but that does not work either....
    can somebody help me?