Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kwr800 · Feb 23, 2021 at 09:17 PM · 2d gameprojectile

How to make a projectile keep moving forward and then destroy itself when out of the map

So basically I wanted to make a game where you take control of a pirate on a boat that tries to avoid cannonballs shot at him.

 using System.Collections; using System.Collections.Generic; using UnityEngine;
 
 public class Projectile : MonoBehaviour {    public float speed;
 
     private Transform player;
     private Vector2 target;
 
     void Start(){
 
         player = GameObject.FindGameObjectWithTag("Player").transform;
 
         target = new Vector2(player.position.x, player.position.y);
 
     }
     
     void Update(){
 
 
 
         transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
        
         
     }
 
 
     void OnTriggerEnter2D(Collider2D other){
         if(other.CompareTag("Player")){
 
             DestroyProjectile();
         }
 
 
     }
     void DestroyProjectile(){
 
         Destroy(gameObject);
     } }

I used this code to make projectiles go after the player but I'm not sure how do I make them keep moving forward after reaching their destination. I also want them to destroy themselves after going out of the map.

Comment
Add comment · Show 1
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 kwr800 · Feb 23, 2021 at 10:41 PM 0
Share

Ok I just decided to destroy the projectile after some time since it'll fly out of the player's view anyways so I added this:

 private float destroyTime = 4;
 
     void Update(){
             Destroy(gameObject, destroyTime);





1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kwr800 · Feb 23, 2021 at 09:47 PM

 Vector3 motion;
  void Start()
  {
      Vector3 sourceToDestination = player.position - transform.position;
      motion = sourceToDestination.normalized * speed;
  }


Ok I added another vector and replaced the void Update with this :

 void Update()
  {
      transform.position += motion * Time.deltaTime;   
  }

It seems to go in a line after achieving it's original destination but I still don't know how to make it disappear after going out of the map.

Comment
Add comment · Show 3 · 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 bigvalthoss · Feb 23, 2021 at 09:54 PM 0
Share

Do you have a map boundary? Like a box that is the boundary? If so you could add a collider on it and delete the object that enters if it has the tag "arrow"

avatar image kwr800 bigvalthoss · Feb 23, 2021 at 09:57 PM 0
Share

I surrouded the map with 4 box colliders. Can you please explain in a bit more detail how to delete the objects that come into contact with them?

avatar image Llama_w_2Ls kwr800 · Feb 23, 2021 at 10:58 PM 0
Share

You can attach a script either to the boundaries or the objects, that use OnTriggerEnter, to detect a boundary. If the trigger's tag is set to boundary, destroy itself. For example, if you were to put a script on the object:

 void OnTriggerEnter(Collider other)
 {
      if (other.gameObject.CompareTag("Boundary"))
      {
           Destroy(gameObject);
      }
 }

@kwr800


Destroying it after some time works fine too, but I would recommend that you try the more solid solution. You never know what spaghetti you might have to end up untangling later down the line, when your bullets can't reach the other side of a long map.

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

150 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

Related Questions

How can i fix this shooting code in a 2D plane? 0 Answers

My code wont work for a projectile following a target 0 Answers

How to make a projectile in the finger/mouse position in a 2D endless runner? 0 Answers

How to make projectiles shoot diagonally? 1 Answer

How to have player fire projectile in a parabola? 4 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