• 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 zarriel · Apr 25 at 01:07 PM · cameraraycastshootingprojectile

My projectile shots wrong with Raycast

I have a problem shooting with Raycast. I am using CineMachine and the shot is tilted, by eye, by the same amount as the camera is tilted. Projectile goes in the correct direction but does not hit exactly where the cursor is. I have no idea why, as I also use the laser with the same settings and everything works fine.

         Vector3 mousePos = Mouse.current.position.ReadValue();
         mousePos.z = fpsCam.nearClipPlane;
         Vector3 worldPosition = fpsCam.ScreenToWorldPoint(mousePos);
         Plane plane = new Plane(Vector3.up, 0);
 
         float distance;
         Ray ray = fpsCam.ScreenPointToRay(Mouse.current.position.ReadValue());
         if (plane.Raycast(ray, out distance)) { worldPosition = ray.GetPoint(distance); }
 
         RaycastHit hit;
         Vector3 targetPoint;

        // I know this is wrong, but on any other settings it didn't work better.
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.position, out hit, 100, layer)) { targetPoint = hit.point; }
         else { targetPoint = ray.GetPoint(100); }
 
         direction = targetPoint - attackPoint.position;
 
         if (Keyboard.current.qKey.wasPressedThisFrame)
         {
             GameObject gObj = Instantiate(prefab, attackPoint.position, transform.rotation);
 
             gObj.TryGetComponent(out Rigidbody rigid);
             rigid.AddForce(direction.normalized * 10f, ForceMode.Impulse);

I tried a dozen or so different changes using variable cameras ViewportToScreenPoint etc., but in the end I can't aim at the ground and the projectile is flying along the terrain or shooting in random directions. Layers are set to Enemy and Ground only. Everything is fine in the scene.

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 Hellium · Apr 24 at 10:16 PM 0
Share
      if (Physics.Raycast(ray.origin, ray.direction, out hit, 100, layer)) { targetPoint = hit.point; }
      else { targetPoint = ray.GetPoint(100); }
avatar image zarriel Hellium · Apr 25 at 11:58 AM 0
Share

It doesn't change anything unfortunatly. Projectile flies along the terrain, but when I changed layers on Nothing it works like my code with correct layers. When I set layer to check "Enemy", projectile flies above Enemy, but enemy collider isn't bigger than game object.

0 Replies

· Add your reply
  • Sort: 

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

244 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 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 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 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

Raycasr in my fps? 1 Answer

raycast spawn problem 0 Answers

Unity Raycast not accurate 0 Answers

Air Strike shoot aiming at cross hairs in middle of the screen 2 Answers

Projectile Hit Detection with Bullet Drop 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