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 /
avatar image
0
Question by McYellowBird · Jul 11, 2019 at 03:59 AM · raycastraycastingfirst person

Raycast not hitting right spot

I've got a problem with a game I started making. Basically every time I shoot, the particle effect which essentially shows where the raycast is hitting, isn't where my crosshair is pointing. My crosshair is in the center of the screen.
Sometimes it'll work and hit the center.
When I try to attach a photo it says "Error parsing the uploaded file.", but basically the ray ends up either hitting a meter or two diagonally left of wherever I'm shooting or on top of the object I'm trying to shoot. Tried messing around with ViewportToWorldPoint. Hasn't made a difference

Btw, pretty damn new at this.

Code: { public float damage = 10f; public float range = 10f; public float fireRate = .5f; private float nextFire;

 public Camera playerCam;
 public ParticleSystem muzzleFlash;
 public GameObject impactEffect;

 public GameObject crosshair;

 void Update()
 {
     if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
     {
         nextFire = Time.time + fireRate;
         Shoot();
     }

 }

 void Shoot()
 {
     Vector3 rayOrigin = playerCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
     muzzleFlash.Play();

     RaycastHit hit;
     if (Physics.Raycast(rayOrigin, playerCam.transform.forward, out hit, range))
     {
         Debug.Log(hit.transform.name);

         Enemy enemy = hit.transform.GetComponent<Enemy>();

         if (enemy != null)
         {
             enemy.TakeDamage(damage);
         }

         GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(impactGO, 2f);
     }
 }

}

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 pmerilainen · Jul 11, 2019 at 09:31 AM

you could try to replace zero z with camera.nearClipPlane

Comment
Add comment · Show 4 · 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 McYellowBird · Jul 11, 2019 at 10:07 AM 0
Share

How would I implement that into my code? I really only somewhat know the very basics

avatar image pmerilainen McYellowBird · Jul 11, 2019 at 10:36 AM 0
Share

playerCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, playerCam.nearClipPlane))

avatar image McYellowBird pmerilainen · Jul 11, 2019 at 10:56 AM 0
Share

I've just added that to my script. Seems to be working so far. Thanks :)

avatar image McYellowBird · Jul 11, 2019 at 10:33 AM 0
Share

God. This is both annoying and slightly embarrassing. Opened up unity after about 5 hours. Shooting works fine now?

avatar image
0

Answer by Bunny83 · Jul 11, 2019 at 11:03 AM

Your whole ViewportToWorldPoint is pointless when you use a perspective camera. Just use the camera position as origin. As for the direction, only the center point will be transform.forward for a perspective camera. It's generally simpler to use Camera.ViewportPointToRay to get a ray for a certain viewport point. For perspective cameras, viewport points off center will have a different direction than transform.forward. Of course for orthographic cameras all rays are parallel.

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

159 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

Related Questions

Raycast and First Person Controller 2 Answers

Can't find a clone object with RayCast? [Solved] 2 Answers

How can I differentiate between colliding with an object or its child? 1 Answer

raycast in direction of movement key down 2 Answers

RaycastHit returns object without collider, in wrong layer 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