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 /
This question was closed Apr 17, 2016 at 06:31 PM by Fujitaka for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Fujitaka · Apr 12, 2016 at 02:30 PM · raycastmouseposition

[2D] Raycast points to 0,0,0 instead of mouse...

Hey Guys. I'm a bit of a noob and I need some advice since google didn't help me. I'm working on a 2D platformer with shooting mechanics, and I'm trying to draw a raycast from the player to my mouse. The problem is, it only points from the player (or rather the used game object inside the player) to the coordinates 0,0. How can I fix that? I'm using Unity 4.7.1f1 btw. Don't ask why, I just like it better.

It tells me that it does shoot, but not that it collides with anything (which is not really surprising).

If you need more information, I will provide it as fast as I can!

here's the code:

void Shoot () { Debug.LogError("Shoot!");

     Vector2 mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     
     Vector2 firePointPos = new Vector2(firePoint.position.x, firePoint.position.y);
     
     RaycastHit2D hit = Physics2D.Raycast(firePointPos, mousePos-firePointPos, 100, whatToHit);
     
     Debug.DrawLine (firePointPos, (mousePos - firePointPos) * 100, Color.cyan);

     if (hit.collider != null) 
     {
         Debug.LogError("Collision");

         Debug.DrawLine(firePointPos, hit.point, Color.red);
     }
     
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by Fujitaka · Apr 17, 2016 at 05:28 PM

After a very long time searching for help, a nice guy in the Discord help chat firgured out a solution. I can't really explain what the problem was and why this code works, since he couldn't even explain it but here's the code:

 void Shoot()
 {
     Vector3 mouseScreen = Input.mousePosition;
     Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(mouseScreen.x, mouseScreen.y, 10.0f)); // Turns the mouses position on the screen into a point in world space.
     cursorPos = mousePos;
     Debug.Log (mousePos.x + "." + mousePos.y);
     RaycastHit2D hit = Physics2D.Raycast(transform.position, mousePos, 100, whatToHit);
     FireBlob ();
     Debug.DrawLine(transform.position, mousePos, Color.cyan);


     if (hit.collider)
     {
         Debug.Log("Collision");
         Debug.DrawLine(transform.position, hit.point, Color.red);
     }
 }

 void FireBlob ()
 {
     Instantiate (blobPrefab, firePoint.position, firePoint.rotation);
 }

 // An IEnumerator is somewhat complex internally but very simple to understand, just remember that "yield return new WaitForSeconds(X)" will cause it to be delayed by X amount of seconds.
 IEnumerator FireWait()
 {
     yield return new WaitForSeconds(timeBetweenShots);
     canFire = true;
 }

Hope that helps someone out there ^^

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

Follow this Question

Answers Answers and Comments

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

Related Questions

I'm trying to use Physics.Raycast to rotate the player to face the mouse cursor but it's not working like it should 0 Answers

My Ray is going the wrong way 1 Answer

Raycast not drawing where i tell it 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

ScreenPointToRay generate wrong ray because camera have a parent with transform 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