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
0
Question by $$anonymous$$ · Sep 07, 2015 at 09:20 AM · mouselookray cast

Shooting towards mouse cursor

Trying to have a mouse shoot the ball at the cursor in a top down 3d game (not 100% top down, closer to diagnal down).

There is a laser pointer on the player so you know where they are aiming, and they shoot along that laser pointer path.

This works initially:

 Vector3 lookAt = camera1.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (transform.position - camera1.transform.position).magnitude));
 lookAt.y = this_laser_pointer.transform.position.y;
 transform.LookAt(lookAt);

But when the player moves at all, they start looking up/down and the player angle is changed, which results in the aiming being off. The rotation constraints don't seem to effect this. Is there a way to have lookAt result in only looking at that one axis to keep the player's aim even?

This code corrects the player angle and aiming issues, but the player's facing direction has a very hard time following the mouse cursor:

 Vector3 lookAt = camera1.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (transform.position - camera1.transform.position).magnitude));
 lookAt.y = this_laser_pointer.transform.position.y;
 motor.facingDirection = lookAt;

Thank you in advance.

Comment
Add comment · Show 3
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 maccabbe · Sep 07, 2015 at 01:13 PM 0
Share

http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

avatar image $$anonymous$$ · Sep 08, 2015 at 01:56 PM 0
Share

maccabbe, I tried that example code you linked, but unsure what it is supposed to do in relation to this. It didn't seem to do anything. Can you explain why you linked that reference?

avatar image Eno-Khaon · Sep 08, 2015 at 11:37 PM 0
Share

ScreenPointToRay is used in conjunction with a Raycast to find where in your world your mouse position is pointing towards.

By facing your character (approximately) towards that location, you're aimed at the spot the mouse position is hovering over.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Sep 08, 2015 at 09:02 PM

maccabees linked code creates a ray you can use to do a raycast with.since your game camera is at an angle you need to somehow map the cursor position into the world in relation to the player. with the raycast you could for example hit the ground in front of the player, which gives you a better position to look at. this position, with the y value if the players position then aligns your aim with the player:

  • ScreenPointToRay

  • Physics.Raycast

  • result.y = player.position.y

that's the steps

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 $$anonymous$$ · Sep 09, 2015 at 12:43 PM

Thank you @hexagonius for the explanation.

I just found this answer on another forum which seems to work well:

 Vector3 lookAt = camera1ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (transform.position - camera_ball.transform.position).magnitude));
 lookAt.y = this.transform.position.y;
  this.transform.LookAt(lookAt) ;

I was using the laser pointer y instead of the player y, and the lookat ignored the collision constraints of the rigidbody which was allowing the player's rotation to go whacky.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

disabling a script (MouseLook) 5 Answers

Help with My C Sharp Script ???? 1 Answer

Disable all instances of a component 2 Answers

Camera suddenly shakes rapidly 0 Answers

Top-Down Game Movement Problem 0 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