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

8 directional shooting for top down 2D shooter?

Discussion in 'Scripting' started by topherbwell, Feb 1, 2016.

  1. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Hello all,
    Currently in a 2D top down shooter, I am using the following script on an touchscreen joystick to shoot in whichever direction the player presses the stick. This works perfectly, but I've noticed it can be a bit tricky to aim like this when using a touchpad instead of a physical joystick.

    What I would like to do is sacrifice a bit of precision to help the player become more accurate, by only allowing the player to fire in 8 directions instead of any angle.

    My current code looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.  
    8.     public float attackSpeed = 0.5f;
    9.     public float coolDown;
    10.     public float projectileSpeed = 500;
    11.  
    12.     public ObjectPooler projectilePool;      
    13.  
    14.     void Update ()
    15.     {
    16.         transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, Mathf.Atan2(CrossPlatformInputManager.GetAxisRaw ("HorizontalShoot"),CrossPlatformInputManager.GetAxisRaw ("VerticalShoot")) * Mathf.Rad2Deg);
    17.  
    18.         if(CrossPlatformInputManager.GetAxisRaw ("HorizontalShoot") > 0.5f ||CrossPlatformInputManager.GetAxisRaw ("HorizontalShoot") < -0.5f)
    19.         {  
    20.             if(Time.time >= coolDown)
    21.             {
    22.                     Fire();
    23.             }
    24.         }
    25.         if(CrossPlatformInputManager.GetAxisRaw ("VerticalShoot") > 0.5f ||CrossPlatformInputManager.GetAxisRaw ("VerticalShoot") < -0.5f)
    26.         {
    27.            
    28.  
    29.             if(Time.time >= coolDown)
    30.             {              
    31.                     Fire();
    32.             }
    33.         }
    34.     }
    35.  
    36.     void Fire()
    37.     {
    38.         GameObject newProjectile = projectilePool.GetPooledObject();
    39.         newProjectile.transform.position = transform.position + transform.up *1f;
    40.         newProjectile.transform.rotation = transform.rotation;
    41.         newProjectile.GetComponent<DestroyOverTime>().lifetime = newProjectile.GetComponent<DestroyOverTime>().lifeTimeStore;
    42.         newProjectile.SetActive(true);
    43.         newProjectile.GetComponent<Rigidbody2D>().AddForce(transform.up * projectileSpeed);
    44.  
    45.         coolDown = Time.time + attackSpeed;
    46.     }
    47. }
    What do you think would be the best way to accomplish this? Thanks!
     
  2. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Get angle in degrees, divide by 45 and cast the result to int then multiply back up or something? should snap.
     
    topherbwell likes this.
  4. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    That sounds good, I just wasn't sure whether I needed to approach this from a different angle (no pun intended) rather that add to what I already have here. I'll see if I can figure out how to do this; this is my first time using the atan2 and rad2deg, so I'm still trying to wrap my head around that bit of trig.