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
1
Question by Nathaniel · Apr 23, 2010 at 05:27 PM · projectilecursorthird-personaim

Aiming in the direction of cursor in third person.

I have a third person game where the cursor will seldom be settled in the middle of the screen, however the character is always centered in the middle of the screen (more or less). I need to be able to shoot projectiles in the direction of the cursor from my character model. I think I may have to use a dummy object as an "aim target" that follows the mouse, but I'm lost on how to implement this. Thanks in advance!

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

2 Replies

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

Answer by Molix · Apr 23, 2010 at 06:35 PM

You can use a Raycast through the mouse position to find out what 3D geometry the mouse pointer is over, and then create your projectile at your player's location, heading toward that point. e.g.

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) 
{
  // Get your player object's position (could be any way, this is just one possible way)
  var playerT = GameObject.FindWithTag("Player").transform;
  // Now you have where your player is and a 3D point the mouse is over
  Debug.DrawLine (playerT.position, hit.point);
}
Comment
Add comment · Show 5 · 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 Nathaniel · Apr 23, 2010 at 11:29 PM 0
Share

This worked nicely, thank you!

avatar image Nathaniel · Apr 23, 2010 at 11:42 PM 0
Share

I have one quick question regarding this actually, how would I go about instantiating a projectile to travel along the line that we've created?

avatar image Molix · Apr 24, 2010 at 03:30 AM 0
Share

$$anonymous$$ake a projectile prefab with a rigidbody in it. Then something like:

GameObject projectile = Instantiate(projectilePrefab, playerT.position, Quaternion.identity) as GameObject; projectile.transform.LookAt(hit.point); projectile.rigidbody.velocity = projectile.transform.forward * projectileSpeed; Destroy(projectile, 2);

avatar image Nathaniel · Apr 24, 2010 at 03:49 AM 0
Share

Unfortunately that doesn't seem to work for me. $$anonymous$$y projectiles seem to jet off in undesirable angles still. Any other thoughts? Sorry to be such a trouble! Here is the exact code I'm using: var playerT = GameObject.FindWithTag("FirePoint").transform; Debug.DrawLine (playerT.position, hit.point); var clone = Instantiate(projectile, playerT.position, Quaternion.identity) as GameObject; clone.transform.LookAt(hit.point); clone.rigidbody.velocity = projectile.transform.forward * 100;

avatar image Nathaniel · Apr 24, 2010 at 10:30 PM 0
Share

Never$$anonymous$$d, I got it! Thanks again for your help :)

avatar image
0

Answer by Ashkan_gc · Apr 23, 2010 at 06:59 PM

for more info: you can get the direction of mouse pos and middle of the screen vector using Vector2 class methods. also if you want to know what point in 3d space mouse is on, then you can use camera.ScreenToWorldPoint as z argument you should get the distance from the camera that you want x,y to be calculated. then you can use that point to look at it and place a target object in the position of the mouse if needed. i think in your game you just need to get the direction between mouse position and center of the screen and then use that direction for (y axis rotation of your player) see the evac city tutorial for an example

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

No one has followed this question yet.

Related Questions

Third person camera aim 0 Answers

my gun wont follow my mouse 0 Answers

Flamethrower!!1 How to aim emitter at cursor 0 Answers

Reticle orbits around player depending on right stick direction? 0 Answers

2.5d game gun aim 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