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 Will748 · Aug 01, 2012 at 05:59 PM · raycastmouse aim

Mouse aim raycast

Hi, I am trying to use the mouse to aim my raycast, its for a 2d platform game, it is working however is inaccurate, for example when I aim left it will shoot a bit higher than the mouse, I can instantiate objects with the mouse accurately in a test script but not aim accurately with raycast. Here is my code:

 var distanceFromCamera = 5.0;
 var distance = 10.0;
 var prefabGrapple:Transform;
 var xOffset = 0;
 var yOffset = 0;

 function Update ()
 {
  //on mouse click ...
  if(Input.GetButtonDown("Fire1"))
  {
      var mousePosition = Input.mousePosition;
      mousePosition.z = distanceFromCamera;
      var worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
      worldMousePosition.x -= xOffset;
      worldMousePosition.y -= yOffset;
      var hit : RaycastHit;
 
      if(Physics.Raycast(transform.position, worldMousePosition, hit, distance))
      {
          var InstanceGrapple = Instantiate(prefabGrapple, hit.point, Quaternion.identity); 
          Debug.Log(worldMousePosition);
 
          if(Input.GetButtonDown("Fire2"))
          {
              Destroy(InstanceGrapple);
          }
      }
  }
 }

Also, how would I destroy the gameobject ?, the below code doesn't seem to destroy the game object.

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
0

Answer by DESTRUKTORR · Aug 01, 2012 at 07:24 PM

     function Update ()
     {
     
          //on mouse click ... 
          if(Input.GetButtonDown("Fire1")) 
          { 
              var mousePosition = Input.mousePosition; 
              mousePosition.z = distanceFromCamera; 
              var worldMousePosition = Camera.main.ScreenToWorldPoint(mousePosition); 
              worldMousePosition.x -= xOffset;
              worldMousePosition.y -= yOffset; var hit : RaycastHit;
     
              if(Physics.Raycast(transform.position, worldMousePosition, hit, distance)) 
              { 
                 impactPoint = hit.point; 
                 var InstanceGrapple = Instantiate(prefabGrapple, hit.point, Quaternion.identity); 
                 Debug.Log(worldMousePosition);
              }
         }
         else if(Input.GetButtonDown("Fire2")) 
         { 
                 Destroy(InstanceGrapple);
         }  
     }

Try this, first. Your flow control statements (if statements) were all mashed together. I imagine you wanted them separate? You may want to look into standard formatting practices (such as separating lines at least at semicolons, and proper indenting). It would make it a lot easier to read and understand what you're doing wrong. However, you may want to try using a ray that casts from the player object to the mouse, in stead... since it would basically give you a precise trajectory, right away.

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 Will748 · Aug 01, 2012 at 11:23 PM

Omg all the formatting went completely wrong, the preview looked fine but has come out different (ill edit now), yeah what I have done is cast a ray from the player object and trying to cast it through the mouse, however always seems a bit off, the accuracy is also inconsistent e.g. sometimes by a 1 cm, sometimes by 0.5 cm.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrow not shooting the in right orientation 0 Answers

Use transform.LookAt with Raycast with a delayed bullet? 1 Answer

GameObject once walked on, gives message using C#? 1 Answer

Raycasting2D please help 1 Answer

how do i fix this small error 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