Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Lemexis · May 16, 2016 at 02:21 PM · c#instantiate prefabtop down shooterrelative position

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 :

alt text

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.

picture.png (37.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vittu1994 · May 16, 2016 at 02:56 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lemexis · May 16, 2016 at 03:05 PM 0
Share

Thank you very much, that clever trick was simple to implement and effective ! I'm updating my original post right now with your solution.

avatar image vittu1994 Lemexis · May 16, 2016 at 04:49 PM 0
Share

Glad to help ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing material dose not change the appearance of the game object 0 Answers

How do I destroy an instantiated object with a mouse click? 1 Answer

Raycast doesn't hit instantiated object within same frame of instantiation. 0 Answers

Trying to add a delay to an Instantiate in a For Loop 0 Answers

Objects position relative to another objects rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges