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 /
  • Help Room /
avatar image
0
Question by Wixxi · Dec 11, 2015 at 03:17 PM · raycastdebugmousepositionscreentoworldpoint

2.5D look at mouse

Hi,

I've been searching and searching and searching forever now.

And i can't for the love of God figure this out. I'm probably extremely retarded. But please i need to get this to work and i'm about to commit suicide. (not really) :P

Anyway, here is my problem. I'm making a 2.5D game. This means that the game is 3D, but viewed from the side like a 2D platformer.

What i want to do is have the weapon of my hero always look at a point on a plane with which every object in the gameworld is alligned. This plane acts as a raycast detecter and whenever i want to fire something the bullet should travel from the origin point of the weapon to the point that is created on the plane where my cursor is aiming on the screen.

What i have so far...

     //raycast
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     Debug.DrawRay(ray.origin, ray.direction * 999, Color.yellow);


     if (Physics.Raycast(ray, out hit,1000f,mask)) {
     
         //AimIndicator.transform.LookAt(hit.transform.position);
     
         if (Input.GetButton("Fire1")) {
             Instantiate(test, hit.transform.position, Quaternion.identity);
         }
     }

The problem with this code is that whenever i click to instantiate the object (test) it always instantiates it at a certain point. Regardless of where my mousepointer is.

Now, i have debugged the ray to see where it is going and as far as i can see the ray is shoting exactly where my mouse is aiming so everything seems to work perfect according to the debug.

However, this does not seem to be the case as my object does not instantiate on the hit point.

I have no idea why??!!

Can anyone please help me with this madness. I'm about to go insane i think.

I can program almost everything without much effort. But whenever there's a raycast involved i'm having so much difficulty. Why are raycasts so extremely difficult to understand? Does anyone have like a solid good tutorial so that i can finally wrap my head around this subject?

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

1 Reply

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

Answer by Wixxi · Dec 09, 2015 at 11:50 PM

O M G.... Just found it..

You are not supposed to ask the transform of a hit. But it's 'point'. The point you get from the hit is also a Vector3 which you can then use to instantiate, whatever...

Yup, i'm retarded. It's confirmed.

Like this..

 if (Physics.Raycast(ray, out hit,1000f,mask)) {
         
     Vector3 instantiatePos = hit.point;
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move object to mouse position 1 Answer

Raycast not drawing where i tell it 1 Answer

My raycasting isn't working 1 Answer

Please help, there is no error at the console tag but the character don´t rotate or react with the mouse position. 0 Answers

Debug.DrawRay isn't working at all 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