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 SPIGS · Dec 29, 2013 at 09:41 PM · c#2dmouseshootingprojectile

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

(EDIT): Ok, I'm creating a 2-d game. I would like my character to be able to shoot projectiles from a spawnpoint in front of him in the direction of the cursor upon clicking on the screen.

alt text

I can already spawn the projectile at the spawn point and apply forces to it and make it move. However I do not know how to find the angle for it to shoot at the cursor.

Answers Appreciated.

diagram.png (6.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by MagicoCreator · Dec 30, 2013 at 03:28 AM

you can calculate the direction from which to shoot through simple calculations:

 function OnMouseDown(){
    var ShootDir : Vector2 = Vector2(Input.mousePosition.x - character.transform.position.x,Input.mousePosition.y - character.transform.position.y) 
 }

then instantiate your projectile, and either add force to move it with physics, or just move it in that direction

as for charging up:

if you want your shot to charge, while the user is pressing the button:

 function Update(){
    var timer : float = 0;
    var isCharing : boolean = false;
    if(isCharging){
       timer += Time.deltaTime;
    }
 }
 
 function OnMouseDown(){
    isCharing = true;
 }
 
 function OnMouseUp(){
    isCharing = false;   
    //shoot with strenght of timer's value
    timer = 0;
 }

if you want to just add a simple delay:

 function Update(){
    var timer : float =  0;
    var willShoot : boolean = false;
    if(willShoot){
       if(timer<0){
       //shoot projectile
       willShoot = false;
       }
       else{
          timer -= Time.deltaTime;
       }
    }
 }
 
 function OnMouseDown(){
    if(!willShoot){
       willShoot = true;
       timer = 0.5f;
    }    
 }
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 SPIGS · Dec 30, 2013 at 05:37 PM 0
Share

It didn't work. And not because the typing errors you made (you put "charing" ins$$anonymous$$d of "charging"). On$$anonymous$$ouseDown() only works when clicking a collider. I want to be able to click anywhere and shoot in that direction. So I put the $$anonymous$$ouseDown part in the Update with Input.Get$$anonymous$$ouseButtonDown(), however it still didn't work. After a good hour of fiddling around with it I finally came up with a way of doing it. Thanks for the answer.

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

19 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

Related Questions

Shooting projectiles in 2D world 2 Answers

Network Sync Bullet 2D 0 Answers

How to get rotation for projectile to fire at cursor? 1 Answer

If i click on a object i move to the next level (c#) 0 Answers

How to make 3 bullets fire at different angles 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