Spawning a projectile in front of a player based on player rotation

Answer : As vittu1994 suggested, I created an empty GameObject that was the child of the Player GameObject, and located where I wanted the projectile to spawn (in front of the gun). Then I made two changes in my code :

  • I created a new public GameObject variable to hold the child object (that I called laserSpawner).
  • In my fire() method, I now instantiate the projectile at the position of the laserSpawner

I updated the code in this post to account for these changes.


Hello,

I am working on a Top Down 2D shooter, and am currently stuck on the player controller. So far, my player moves based on keyboard input, rotates based on mouse input, and the camera follows the player around. The problem is with my Fire method.

As expected, the projectile is spawned in a position relative to the player position, with the player’s rotation, and will move in the direction the player is looking… But I have no idea how to make my code take into account the player’s rotation when determining the spawn position of the projectile.

Here is a picture which will, I hope, make it clearer :

70179-picture.png

On the left picture, when the rotation of my player is (0, 0, 0), it works fine. On the right picture, however, when the rotation of my player is (0, 0, 50), I would like the laser to always spawn in front of the gun (just as on the left picture).

Here is my code. The Fire() method is at the end, it’s the last one (I still put the entire code there just in case):

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed = 5.0f;
    public float laserSpeed = 5.0f;
    public GameObject playerLaser;
    public GameObject laserSpawner;
    public float xLaserOffset;
    public float yLaserOffset;

    private Rigidbody2D playerRigidbody;
    private Vector2 movement;
    private Vector2 currentPosition;
    private Vector3 mousePos;

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

	void FixedUpdate ()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        Move(horizontal, vertical);
        Turn();

        if (Input.GetButtonDown("Fire1"))
        {
            Fire();
        }
    }

    void Move (float horizontal, float vertical)
    {
        movement.Set(horizontal, vertical);
        movement = movement.normalized * speed * Time.deltaTime;
        currentPosition = transform.position;
        playerRigidbody.MovePosition(currentPosition + movement);
    }

    void Turn()
    {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
    }

    void Fire()                     // WHERE I INSTANTIATE THE LASER
    {
                GameObject laser = Instantiate(playerLaser, 
        laserSpawner.transform.position, transform.rotation) as GameObject;
    laser.GetComponent<Rigidbody2D>().velocity = transform.up * laserSpeed;
    }
}

By the way, it’s the first time I post something here. I searched google extensively (which helped me a lot when writing the controller) but couldn’t find an answer to that specific question (except in one post from 2005 which gave a complicated answer before Unity supported 2D, so I guess there is a simpler way to do it now). I then read the guidelines to be sure my post would follow the conventions here, but if my post isn’t right according to Unity Answers conventions, please tell me so that I may ask better questions in the future (or change this one if needed).

Thank you.

One trick is to create a empty gameobject that just exists so that you can access its Transform. Put this empty gameobject as a child to the player object and position it so its infront of your gun. As long as it is the child it will move around with the player object (not sure about rotating but it seems you know how to do it).

Then whenever you need to instantiate this laser u can use the transform.position of the empty gameobject and the rotation of the player.