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 jimmy14mac · Dec 26, 2013 at 11:12 PM · raycastlookattheoryhack

"Hacking" the lookAt function

So recently I've been delving into topdown shooter controls for a new project and was reading up on a shooting script when I came across something user "aldonaletto" mentioned that peeked my interest. He said:

Since you said ship, I suspect that you're clicking the empty space - and this won't work, because Raycast only returns true if some collider (at 100m or less, in this case) is hit. If this is the case, a possible hack is to create a big plane (scale 10000,1,10000) in front of the ship at a big distance (5000, for instance) and delete its mesh renderer. Child the plane to the player, and remeber also to change the raycast to Physics.Raycast(ray, hit) to remove the distance limit.

My question is, is this a legit way of getting this done? Now obviously, if something works, it works, even if it is the tiniest bit gimmicky, but I guess what I'm asking is, is this a "good" way to get the LookAt functions to work in this context? Is there a better way to make things work or give the mouse something to "hit"? Personally, I've been using a rotation script based on player and mouse positions, but LookAt just seems like the better way to go, especially in a top down shooter.

Cheers!

Comment
Add comment · Show 2
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 T27M · Dec 26, 2013 at 11:22 PM 1
Share

I remember reading from someone on here that games are made of smoke and mirrors. To me that means if it works, it's easy to maintain, and it doesn't effect performance then its a decent "hack" imo.

One way I got round the empty space problem was to cast a ray from the mouse to screen and use that direction as a look at position, not sure if that would work in your case.

avatar image jimmy14mac · Dec 26, 2013 at 11:30 PM 0
Share

Hey, I appreciate the reply! That's exactly my thought too, smoke and mirrors.

And that's essentially what I was going to do but had an issue with the Z axis messing things up, which what I think the plane would fix.

2 Replies

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

Answer by robertbu · Dec 26, 2013 at 11:29 PM

Seems fine to me, though I think there are a couple of better solutions. At a minimum, you can use Collider.Raycast() instead of Physics.Raycast(). Collider.Raycast() allows you to cast just against the plane instead of all the objects in the scene. A better yet solutions is to use Unity's mathematical Plane. Then you can use Plane.Raycast(). This solution involves no meshes, does not have issues with the the size of the plane, and is more efficient. If the plane is parallel to the camera plane (i.e. the camera is not at an angle with respect to the plane), then you can use Camera.ScreenToWorldPoint() as an even more efficient solution.

@aldonaletto knows all of this, so whatever solution he was outlining was specific to the question or perhaps the simplest solution for the situation.

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 jimmy14mac · Dec 26, 2013 at 11:46 PM 0
Share

Thanks robert! You guys sure seem to know your stuff. Thanks for all the info! I'll definitely check out everything you mentioned.

avatar image
0

Answer by Owen-Reynolds · Dec 27, 2013 at 03:41 PM

It seems to me that the end result you're wanting is their "click arrow." Like, if they were click-firing a laser, this is where it would start and aim. You could then raycast, launch a rocket...whatever, using that click arrow.

As T27M hinted in a comment, the "click arrow" is built in the first step of a ray cast: Camera.main.ScreenPointToRay(Input.mousePosition);

That uses the imaginary front-plane of the camera as a start point (no need for you to make it.)

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

20 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 avatar image

Related Questions

Transform.LookAt Seems To Cause Issues 1 Answer

Camera rotate to look at GameObject from Raycast 3 Answers

Aim flashlight at cursor in third person 1 Answer

smooth raycast LookAt ? 1 Answer

LookAt To Only Rotate on Y Axis 2 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