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 tarnovsky · Jan 22, 2019 at 03:16 PM · 2d gameshootingprojectile

How do I stop a 2D projectile when it reaches a point

I have a weapon that fires projectiles where you aim with some inaccuracy by getting a vector and I want it to stop close to the point I click but I don't even know where to start, here's my code so far-

 void Start()
 {
     zx = Random.Range(-spread, spread) / 200;
     zy = Random.Range(-spread, spread) / 100;
     Vector2 heading = GameObject.FindGameObjectWithTag("target").transform.position - transform.position;
     vector = heading.normalized;
     // find rotatio
     angle = Mathf.Atan2(heading.y, heading.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(0, 0, angle);
 }
 void Update()
 {
     transform.localPosition += new Vector3(vector.x + zx,vector.y + zy) * speed;
 }


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 TreyH · Jan 22, 2019 at 03:26 PM 0
Share

cleaned the formatting, you don't need to add the ` for pasting blocks of code btw

3 Replies

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

Answer by tormentoarmagedoom · Jan 22, 2019 at 03:48 PM

Good day.

Its simple. I did something like this in the past.

 Make the bullet a prefab, whith a script wich:
 -know the real destination point (we call from now "realdest")
 -Calculate a little random destination for the inacuraccy.
 -Moves the bullet (itself) to this new destination dest.
 -Reads the position of the bullet every frame (or every 0.5 seconds or something liek this)
 -Store this position in a Vector3 variable called "LastPos"
  -Then each update check for this 2 distances (LastPos-realdest) and (tranform.position-realdest)
 -During all bullet travel,  (LastPos-dest) is always higher than (tranform.position-dest) becuase you are approaching to dest
 -But when (LastPos-dest) <  (tranform.position-dest) it means you reached the closest point to the real destination of your trajectory, so you can destriy the bullet (itself)

EASY! :D

Good luck! 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 xxmariofer · Jan 22, 2019 at 03:24 PM

Why not just an if in the update comparing if the transform.localposition is more or equals to the desired position? then just set velocity to 0

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 tarnovsky · Jan 22, 2019 at 03:31 PM 0
Share

I can't use equals because of the inaccuracy or more because you can shoot in any direction

avatar image
0

Answer by R4y-GM · Jan 22, 2019 at 03:43 PM

there are a lot of possibilites to stop it you can add a time.timescale = 0 or a OnCollisionEnter(Collision collision) and put it if(collision.gameobject.tag =="your projectile tag" ) to collide you must do a box with a collider

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

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

118 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

Related Questions

How to shoot a bullet and animate at the same time??? Its a 2D shooter c# 3 Answers

How to make projectiles shoot diagonally? 1 Answer

How do i adjust the projectile path and direction? 1 Answer

2d direcitonal shooting 0 Answers

How can I get my bullet to shoot the other way when I face left. 2d 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