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 OutRage_Studios · Jan 26, 2012 at 12:59 AM · 2dvector3angleprojectilefire

Firing a projectile in 2D

I am attempting to make a 2D catapult that fires at a certain angle depending on where the user drags the mouse cursor to from a starting position.

I have managed to get it semi working with the code below but it provides qute unusual behaviour (the projectile never fires on the correct dragged angle). Any ideas?

var ClickDown : Vector3;

var ClickUp : Vector3;

var point : Vector3;

var pooPrefab : Transform;

var multiplier : float;

function OnMousedown(){

var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

var hit : RaycastHit;

   if (collider.Raycast (ray, hit, 100.0)) {

       ClickDown = hit.point;

   }

}

function OnMouseUp(){

 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

 var hit : RaycastHit;

     if (collider.Raycast (ray, hit, 100.0)) {

          ClickUp = hit.point;

     }

 FirePoo();

}

function FirePoo () {

   var projectile = Instantiate (pooPrefab, ClickUp, Quaternion.identity);

   var shootVector = ClickDown - ClickUp;

   projectile.rigidbody.velocity = shootVector * multiplier;

}

Comment
Add comment · Show 4
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 kevork · Jan 26, 2012 at 01:08 AM 0
Share

Depending on the objects in your scene, you aren't necessarily firing in 2D. Are your ClickUp & ClickDown vectors what you expect them to be?

avatar image OutRage_Studios · Jan 26, 2012 at 01:16 AM 0
Share

Yeh the object fires on the X/Y axis, the problem is that on mouse up the ball fires correctly from the end position, however it does not fire in the dragged angle if that makes sense. Imagine an angry birds style catapult the fires in a random direction (its not meant to though)

avatar image kevork · Jan 26, 2012 at 01:38 AM 0
Share

Since you are using Physics to drive the motion there are a myriad of settings that could cause your motion to be not as expected. Here's a random list of things to try or check.

  • If you want the object to be constrained to the X,Y plane you can constrain the Z position & rotation.

  • You can attempt to use http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html ins$$anonymous$$d of setting the velocity directly.

  • Ensure both the drag and angular drag settings are what you intend.

  • Ensure your gravity is going the direction you intend.

avatar image OutRage_Studios · Jan 26, 2012 at 01:45 AM 0
Share

Unfortunatly it seems none of those work, is there a different way i could replicate this mechanic i am attempting that you know of?

0 Replies

· Add your reply
  • Sort: 

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

How do I fire a 2D projectile based on it's current angle/location. 1 Answer

Calculate third Vector3 with two Vector3s and an Angle 1 Answer

Shooting projectiles in 2D world 2 Answers

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

c# 2D top down, fire projectale which continuously move in a direction 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