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 Namnam · Jan 18, 2017 at 02:01 PM · cameraraycasthitthird-person-shooter

How to shoot a raycast with third person

Hi, I have a third person model with controls for move and a camera look at him and follow. Now i'm looking for make him shoot with a RayCast but I get some problem. The raycast doesn't hit at the crosshair but above or beside and can't adjust it with Vector3 because it change according to the distance.

I have tried to cast a raycast from camera to camera forward and adjust the y of hit.point then i make a raycast from gun to the direction of hit.point but doesnt' work.

I tried the same with a raycast from camera to the camera crosshair.

How make simple third person shooter please because i don't see any tutorial and i wondering what wrong and if it's the good way to do it.

code here:

 RaycastHit cameraHit;
                     Ray cameraAim = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2));
                     Physics.Raycast (cameraAim, out cameraHit, 100f);
     
                     Debug.DrawLine (cameraAim.origin, cameraHit.point, Color.green);
     
                     Vector3 cameraHitPoint = cameraHit.point;
                     float cameraDistance = cameraHit.distance;
     
                     if (Physics.Raycast (cameraAim, out cameraHit)) {
                         // something was hit
                         RaycastHit playerHit;
                         Physics.Raycast (instantiatePoint.position + new Vector3 (0f, 0f, 0f), cameraHitPoint - instantiatePoint.position - new Vector3 (0f, 0f, 0f), out playerHit, 100f);
                         Debug.DrawLine (instantiatePoint.position + new Vector3 (0f, 1.8f, 0f), cameraHitPoint - transform.position - new Vector3 (5.0f, 10, 0f), Color.red);
                         if (Physics.Raycast (instantiatePoint.position + new Vector3 (0f, 0f, 0f), (cameraHitPoint - transform.position) + new Vector3 (0f, 0.8f, 0f), out playerHit, 100f)) {
                             GameObject bulletHoleObject = (GameObject)Instantiate (bulletHole, playerHit.point, Quaternion.LookRotation (playerHit.normal));
                             bulletHoleObject.transform.up = playerHit.normal;
                         }
                         float playerDistance = playerHit.distance;
     
                         Debug.Log ("Distance from player: " + playerDistance);
                         Debug.Log ("Distance from camera: " + cameraDistance);
     
     
                     }
 

Thanks in advance.

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
4

Answer by AurimasBlazulionis · Jan 18, 2017 at 02:21 PM

Shoot a ray from the camera forward. Then, once you have the position, do a Linecast from the weapon to the camera's ray hit point.

You might want to check if the hit is not behind the player. To check that, do: characterTransform.InverseTransformPoint(hit.point).z < 0.

Comment
Add comment · Show 1 · 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 Namnam · Jan 18, 2017 at 03:55 PM 0
Share

I done what you have advised me and i have adjusted the y of the point of the raycast of the camera in order to match with the crosshair because the camera is above the player and she look at him but the impact of the bullet is above or beside the crosshair according to the distance with the player and camera, so i can't adjuste the point of the raycast of the camera to match with the crosshair. Furthermore, sometime it doesn't make bullet's impact. What is wrong with my code ?

here is my code :

 public void shootOne() {
         if (Time.time > lastShoot + firerate) {
 
             Vector3 fwd = Camera.main.transform.TransformDirection(Vector3.forward);
             Vector3 direction = new Vector3 ();
         
             RaycastHit hitCam;
 
             if (Physics.Raycast (Camera.main.transform.position, fwd, out hitCam, 500)) {
 
                 direction = hitCam.point;
 
             } 
             Debug.DrawLine (Camera.main.transform.position, direction, Color.green);
             RaycastHit hit;
 
             Debug.DrawLine (instantiatePoint.position, direction, Color.red);
             direction.y += 18.5f;
             if(Physics.Linecast(instantiatePoint.position,direction,out hit)) {
                 GameObject bulletHoleObject = (GameObject)Instantiate (bulletHole, hit.point, Quaternion.LookRotation (hit.normal));
                 bulletHoleObject.transform.up = hit.normal;
             }
 
             lastShoot = Time.time;
         }
     }
 

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

6 People are following this question.

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

Related Questions

Touch different Gameobjects using camera array and raycast hit 3 Answers

Problem With Raycasts, Interaction Script. 1 Answer

Need help with Third person shooter 0 Answers

Is object at least partly visible? 1 Answer

Can someone modify this for me? 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