• 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
1
Question by Caleb-2 · Mar 14, 2010 at 05:44 AM · mousethird-personshooteraimside-scrolling

Side Scrolling Shooter. How to aim my bullet trajectory at the mouse cursor?

I know this question may sound familiar but I cannot find any answers for this nor can, in my limited experience with unity, wrap my head around how to make this calculation.

Take a look at this project. How do I get the bullets to shoot toward the cursor all the time? Keep in mind that the character isnt necessarily in the middle of the screen.

http://caleb-stamper.com/downloads/2DGameplayTutorial.zip

THANK YOU so much for helping.

For those who'd like a taste of my trouble before downloading:

var projectile: Rigidbody; var speed = 20; function Update () { if(Input.GetMouseButton (0) ) { var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);

     var mousex = Input.mousePosition.x;
     var mousey = Input.mousePosition.y;

     instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,mousey - .5 * Screen.width,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 duck ♦♦ · Apr 22, 2010 at 07:02 PM 0
Share

Hey, any progress on this? Did you get it working?

2 Replies

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

Answer by duck · Mar 14, 2010 at 08:40 PM

I have to say, I didn't download the project. But from the code you posted, something along these lines should do the job. The trick is - to read the current distance from the camera (to the player's ship, or whatever is launching the projectile). Then convert the mouse position to a world position at the same distance away from the camera.

You can then use the normalized difference between these two points as the velocity - multiplied by whatever speed you like.

(untested code - for illustrative purposes, may contain errors, etc!)

var projectile: Rigidbody; var projectileSpeed = 20; function Update () { if(Input.GetMouseButton (0) ) { var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);

     // get current distance from camera
     var distFromCam = Camera.main.transform.InverseTransformPosition(transform).z;

     // get target in world space, at same distance from camera:
     var targetScreenPos = Input.mousePosition;
     targetScreenPos.z = distFromCam;
     var targetPosition = Camera.main.ScreenToWorldPoint(targetScreenPos);

     // calculate direction & velocity:
     var targetDelta = (transform.position - targetPosition);
     var launchVelocity = targetDelta.normalized * projectileSpeed;

     instantiatedProjectile.velocity = launchVelocity;
 }

}

(If all your objects are always at the same fixed distance away from the camera, you could optimise this code by having that as a constant value rather than looked-up using InverseTransformPosition)

Comment
Add comment · Show 4 · 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 TinyUtopia · Apr 20, 2010 at 01:31 AM 0
Share

I get this when trying to use ducks code:

Assets/Scripts/ShooterScript.js(10,39): BCE0019: 'InverseTransformPosition' is not a member of 'UnityEngine.Camera'.

avatar image duck ♦♦ · Apr 20, 2010 at 08:32 AM 0
Share

Ah yes, ok that line shoud be: Camera.main.transform.InverseTransformPosition(transform).z; (edited answer to show this too)

avatar image Caleb-2 · May 07, 2010 at 12:05 PM 0
Share

Sorry it took so long to respond. I was so excited about the solution I completely forgot to respond. The code was general but after working out the syntax and variable names this worked perfectly. Even the z axis was perfect. Thanks a LOT!

avatar image Vinnie035 · May 27, 2010 at 11:33 AM 0
Share

Could you please post the code you used (with the right syntax and variable names) for other users like me ? Thanks in advance.

avatar image
0

Answer by sean · May 06, 2010 at 09:28 PM

I was poking around at the Camera class documentation the other day, and I believe it has built-in functions for turning a screen or viewport coordinate (e.g. a mouse x/y) into a world-space coordinate at a specified depth from camera, or better yet, a ray that works with the collision system.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How To Get Object To Aim at Mouse in Top Down 3D Shooter 3 Answers

Proper mouse aim of bullets with rigidbody2d and side scrolling? 0 Answers

How to throw a RigidBody in the direction of Mouse Pointer (3D)? 1 Answer

What is the best way to do third person melee combat? 1 Answer

aiming with animation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges