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
0
Question by RealNiv · Mar 25, 2020 at 07:18 PM · instantiateprefabsprojectileprojectiles

Projectile disappears instantly after appearing.

So I'm trying to add a projectile system to my game. I've seen a tutorial about it and I don't know why the projectiles I spawn only appear where my player is but then instantly disappears.Additionally, it does not move even if I just place it in the scene. here are the relevant scripts. What did I do wrong?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectiles : MonoBehaviour
 {
     public float speed;
     private Vector2 screenBounds;
     private Rigidbody2D rb;
     
     void Start()
     {
         Debug.Log("MOVING");
         rb = this.GetComponent<Rigidbody2D>();
         rb.velocity = new Vector2(speed, 0);
         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 
         Camera.main.transform.position.z));
     }
 
     // Update is called once per frame
     void Update()
     {
         //transform.position += transform.right * speed * Time.deltaTime;
         
         if(this.transform.position.x > screenBounds.x *-2)
         {
             Destroy(this.gameObject);
         }
     }
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag != "Player")
         {
             Destroy(this.gameObject);
             Destroy(collision.gameObject);
         }
             
     }
 
 }
 

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float speed;
     public GameObject projectile;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetMouseButton(0))
         {
             Shoot();
         }
 
         // The input for at the horizontal axis - Keys A and D.
         float horizontalIInput = Input.GetAxis("Horizontal");  // gets a value of 1 or -1 
         transform.Translate(new Vector3(horizontalIInput, 0, 0)*speed*Time.deltaTime);
 
         // The input for at the vertical axis - Keys W and S.
         float verticalInput = Input.GetAxis("Vertical");  // gets a value of 1 or -1 (up or down)
         transform.Translate(new Vector3(0, verticalInput, 0) * speed * Time.deltaTime);
     }
     void Shoot()
     {
         
         GameObject p = Instantiate(projectile,this.transform.position,Quaternion.identity); 
          //spawns the projectile 
         
     }
 }
 
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
0
Best Answer

Answer by streeetwalker · Mar 25, 2020 at 08:44 PM

this looks suspicious.

 if(this.transform.position.x > screenBounds.x *-2)

You want to check if the bullet goes off the right side of the screen or the left? I'm pretty sure this is checking if your projectile position is > the left side of the screen *2. Because you're multiplying by -2. So, you are destroying it immediately.


I think you have worse problems: have you checked what your screenBounds variable's value is? You're better off converting an objects' world position to screen coordinates and checking against screen.width and screen.height for right and top bounds, and 0 and 0 for left and bottom bounds.

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 RealNiv · Mar 25, 2020 at 09:39 PM 0
Share

Thanks! that was really the problem. I copied this line from the video and it was suspicious to me too. I don't know how did it work for him for the same purpose(destroy the projectile if it passes the right side of the screen).

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

233 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 rotation of prefab 0 Answers

Problem with projectile direction 0 Answers

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers

Is there any good sample code for shooting a projectile and collision detection? 0 Answers

Instantiating prefabs, and making them move. (Projectiles) 0 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