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 Gatsua · Jun 29, 2014 at 05:04 PM · c#2dmovementprojectilefire

c# 2D top down, fire projectale which continuously move in a direction

Hi, I am trying to fire a projectile that should move in a direction until it gets destroyed depending on mouse position.

The following code works but the bullet stops at where I clicked, how can I make this bullet continue on its path until destroyed?

 public class BulletStats : MonoBehaviour
 {
     public int Damage = 1;              // Damage inflicted
     public bool isEnemyShot = false;    // Projectile from player or enemies?
     public Camera camera;               // camera object
     private float speed = 5f;           // bullet speed
     private Vector2 clickPos;           // bullet direction
 
     void Start()
     {
         camera = (Camera) GameObject.FindObjectOfType(typeof(Camera)); // get the camera
         
         // get world location of click
         clickPos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
 
         Destroy(gameObject, 20); // give bullet 20 seconds lifetime
     }
 
     void Update()
     {
         // move the bullet in the right direction
         transform.position = Vector2.MoveTowards(transform.position, clickPos, speed * Time.deltaTime);
     }
 }
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

3 Replies

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

Answer by Kiwasi · Jun 30, 2014 at 08:01 PM

What you need to do is convert the mouse position into a direction first.

Try the following:

 // In the declarations
 private Vector3 shotDirection;
 
 // At line 15
 shotDirection = (clickPos-transform.position).normalised;
 
 // Replace line 22 with the following
 transform.position = transform.position + shotDirection * speed * Time.deltaTime;
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 timvanderweijde · Jun 30, 2014 at 07:55 PM

Do you want the bullet move to the mouse position continuously? You should use the code:

clickPos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));

also in the Update method.

Still, it will stop at the mouse position. What kind of behaviour do you want? Should it pass the mouse position when it's there?

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 timvanderweijde · Jun 30, 2014 at 08:40 PM 0
Share

Don't forget ScreenToWorldPoint is also using the z-as. Which won't work in 2D.

I added some extra lines in your code. (and from Bored$$anonymous$$ornmon)

var camPos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y)); clickPos = new Vector3 (camPos.x, camPos.y, 0); shotDirection = (clickPos-transform.position).normalized;

avatar image
0

Answer by RakshithAnand · Jul 01, 2014 at 06:17 AM

What you need to do: Add a ridgidbody2D component to your bullet. make it isKinematic and fixed angle.

get direction by normalized method. (Like BoredMormon suggested)

 void Update()
 {
     if(Input.GetMouseButtonUp(0))
     {
        // you get clickpos like what you have done and you do this
        Vector3 shotDir = ((Vector3)clickPos - transform.position).normalized;
     
        gameObject.rigidbody2D.velocity = shotDir;
     }
 }
 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Uneven speed in 2d movement script 2 Answers

How do I get my character to shoot a projectile in the direction of the cursor? 1 Answer

Bullet not moving from script 3 Answers

Stop Player movement when bool changes 2D 3 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