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 /
avatar image
0
Question by Supermacka · Aug 28, 2018 at 03:41 PM · movementinstantiateshooting

How do I Instantiate my shot and make it stay infront of the player before releasing the button.

Hey,

I'm building a 2D project where I will have two spaceships that battle to get the most of their own color on the ground (bombit, splatoon, etc. inspired).

I'm stuck at creating a shot mechanic where the player can holds space and the shot Instantiates infront of the player on the "shotPosition" (it charges, gets more powerful while doing this) while the player is moving, until you release the space-button and the shot gets fired away forward.

I have been stuck on this for hours.

Thanks in advance

alt text

(On the picture you can see the spaceShip and shotPos).

Here is my code for the movementScript:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player1_Controller : MonoBehaviour {
 
     //Int and Floats
     public int turnSpeed;
     public float maxSpeed;
 
     public float defaultPower;
     public float charge;
     public float maxPower;
     public float reloadTimer = 1f;
 
     private bool isUp = false;
     private bool canShoot = true;
     // Gameobjects and Transforms
     public Transform shotPos;
 
     // Rigidbody
     Rigidbody2D rb2d;
     public Rigidbody2D shotPrefab;
 
     // Use this for initialization
     void Start ()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void Update()
     {
         float yAxis = Input.GetAxis("W_S") * maxSpeed;
         float xAxis = Input.GetAxis("A_D");
 
         MovementForward(yAxis);
         Turning(transform, xAxis * -turnSpeed);
         Shooting();
     }
 
     private void MovementForward(float amount)
     {
         Vector2 force = transform.up * amount;
 
         rb2d.AddForce(force * Time.deltaTime);
     }
 
     private void Turning(Transform t, float amount)
     {
         t.Rotate(0, 0, amount * Time.deltaTime);
     }
 
     private void Shooting()
     {
         if (Input.GetKey(KeyCode.Space) && charge < maxPower)
         {
             charge += Time.deltaTime * 30;
 
         }
         if (Input.GetKeyUp(KeyCode.Space) && canShoot == true)
         {
             Rigidbody2D shot;
             shot = Instantiate(shotPrefab, shotPos.position, shotPos.rotation) as Rigidbody2D;
             shot.AddForce(shotPos.up * charge);
 
             charge = defaultPower;
             canShoot = false;
 
         }
         else if (canShoot == false)
         {
             reloadTimer -= Time.deltaTime;
 
             if (reloadTimer <= 0)
             {
                 reloadTimer = 1f;
                 canShoot = true;
             }
         }
     }
 }

  


gymnasieprojekt-hjalp.png (56.6 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Aug 28, 2018 at 04:30 PM

Good day.

You only need to this (I will not write the code, i will tell you what to use, look for how to use it correctly)

  • Detect when user mantains button with Input.GetKey and instantiate the bullet as a child of your ship

  • Then detect whn user relases the key with Input.GetKeyUp to unparent the bullet and start its movoment.

Bye!

Comment
Add comment · 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
0

Answer by WybrenAkker · Aug 28, 2018 at 04:45 PM

Hi @Supermacka !

What I would do is Instantiating the object as a child of your shotPosition when spacebar is pressed. (make sure you only spawn it once)

When the player releases the spacebar remove the shot object's parent and add force to it.


This would look something like this:

 Rigidbody2D shot;
 private void Shooting()
 {
     if (Input.GetKey(KeyCode.Space) && charge < maxPower)
     {
         charge += Time.deltaTime * 30;
             
         if(shot == null)
         {
             shot = Instantiate(shotPrefab, shotPos) as Rigidbody2D;
         }
     }
 
     if (Input.GetKeyUp(KeyCode.Space) && canShoot == true && shot != null)
     {
         shot.transform.parent = null;
 
         shot.AddForce(shotPos.up * charge);
 
         shot = null;
         charge = defaultPower;
         canShoot = false;
 
     }
     else if (canShoot == false)
     {
         reloadTimer -= Time.deltaTime;
 
         if (reloadTimer <= 0)
         {
             reloadTimer = 1f;
             canShoot = true;
         }
     }
 }

   

If this does not work for you please let me know.


Best,

Wybren

Comment
Add comment · Show 1 · 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 Supermacka · Aug 28, 2018 at 06:02 PM 0
Share

@WybrenAkker

I came so far as making the shots stay in the same position when the player only rotates, but if i move up/down or the player just glides (because of my floaty movement) the ship leaves the shot. And sometimes I have to press space twice to make it move forward.

     Rigidbody2D shot;
 
         private void Shooting()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && charge < maxPower)
         {
             charge += Time.deltaTime * 30;
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
         {
             if (shot == null)
             {
                 shot = Instantiate(shotPrefab, shotPos.position, shotPos.transform.rotation) as Rigidbody2D;
                 shot.transform.parent = shotPos.transform;
                 shot.transform.position = shotPos.transform.position;
             }
         }
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space) && canShoot == true && shot != null)
         {
             shot.transform.parent = null;
 
             shot.AddForce(shotPos.up * charge);
 
             shot = null;
             charge = defaultPower;
             canShoot = false;
 
         }

I do still appreciate the time you and everyone here take to help a noob like me <3 I need to take a pause, been sitting with this for nealy 2.5 hours. The struggle.

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

157 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 avatar image avatar image avatar image

Related Questions

Bullet stops when new bullet instantiates pls help 3 Answers

My shooting system wont work, help 1 Answer

Shooting with variable vector3 0 Answers

How to make projectiles shoot diagonally? 1 Answer

I cant get my 2d shooting script to work? 2 Answers


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