Search Unity

2D Directional shooting

Discussion in 'Scripting' started by SolarFalcon, Jan 19, 2020.

  1. SolarFalcon

    SolarFalcon

    Joined:
    Nov 28, 2015
    Posts:
    170
    Hello.

    I have been trying to replicate the spell casting/shooting mechanic seen in Bloodstained. For those who haven't played it, Bloodstained is the spiritual successor of the Castlevania series. In this specific game, if the player uses the Right Stick on their controller, they can aim in a 360 degree angle around their character. The game is 2D but uses 3D models. So far, I have the animations set up properly, but that was the easy part. I thought I would use a point on the hand of the Character model to spawn bullets from and just point them in the direction of the hand, but sometimes the hand isn't on the 2D plane, so this would cause the bullets to go the wrong way. How is this done? I'm sure it has something to do with some math I never learned. I'd appreciate it if someone could point me in the right direction. I tried searching for this topic and came up with nothing.

    I really just need to find a way to ensure the bullets spawn where the hands are, but stay on the X,Y and don't move on the Z axis. Hope I explained that clearly. Thanks for reading!
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Hi,
    hard to tell without knowing your exact setup but you can put a spawner GameObject at the desired place and parent it to the hand. Then attach a script that fires the bullets.In the script you can use transform.forward (or up or right, depends on the objects roatation). You then get a vector in world space where the hand is pointing. In that vector you set the z to 0 like: Vector3 correctedDirection=new Vector3(transform.forward.x, transform.forward.y,0f);

    I always have to play around with those kind of things so no guarantee it will work instantly ;)
    But what can help you is after you calculated the correctedDirection you write in your code:
    Debug.DrawRay(transform.position,transform.forward,Color.red);
    Debug.DrawRay(transform.position,correctedDirection,Color.green);

    That way you can visualize both vectors in the editor view and see if something is wrong.
     
    SolarFalcon likes this.
  3. SolarFalcon

    SolarFalcon

    Joined:
    Nov 28, 2015
    Posts:
    170
    I'll give this a try. This seems so simple, not sure why I didn't think of that the first time. Thanks!
     
    Olipool likes this.
  4. SolarFalcon

    SolarFalcon

    Joined:
    Nov 28, 2015
    Posts:
    170
    Code (CSharp):
    1. Vector3 currentShotHeading =  LeftHand.position - ShotHeadingOrigin.position;
    2.  
    3.                     LeftHand.rotation = Quaternion.LookRotation(currentShotHeading);
    4.  
    5.                     GameObject o = Instantiate(testBullet, LeftHand.position, LeftHand.rotation);
    This works as expected. Thanks for the hint!

    *Edit*
    A little more info just in case anyone else has this problem. I made a transform (ShotHeadingOrigin) and childed it to the Player transform. Then I made another Transform (LeftHand) and added a script that makes it follow the left hand, but locks the position.z to 0 as it isn't needed for these purposes. This transform does NOT have a parent. If you are having any problems with the angle of the shots, adjust the ShotHeadingOrigin position and it should help.
     
    Olipool likes this.