Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Andrea90 · May 03 at 03:47 AM · raycastshooterthird-person-camera

RayCast in Third person Shooter

Hi,

Im making a third person shooter and Im using Raycast to detect an hit point and use it to set the destination of the bullet from the muzzle point of the weapon. The origin of my raycast is the center of the camera. The problem here is that in case there is an hit point behind the weapon my bullet go backwards so I want to avoid this behaviour.

I upload two pictures and the script to explain myself better :

         distance = Vector3.Distance(player.transform.position, cam.transform.position);
         Vector2 screenCenterPoint = new Vector2(Screen.width / 2f, Screen.height / 2f);
         Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
         Debug.DrawRay(ray.origin + cam.transform.forward * distance, ray.direction * 1, Color.yellow);
         Debug.DrawRay(ray.origin, ray.direction * 1, Color.red);
         Debug.DrawLine(cam.transform.position, player.transform.position, Color.blue);

The yellow ray is the forward direction starting from the center of the camera but must begin where the position of the gun is. I use the Vector3.Distance to determine how far should the origin's ray start but if the camera goes down or up this distance is not accurate.(Picture2)

The red ray has has the camera position as origin but it will detect objects that are behind.

The blue ray is just a draw Line debug.

In other words I want that my yellow ray starts at the end of the blue ray always How can I achieve this? I hope I make myself clear and I hope u can help me. Thank you

alt text

alt text

picture1.png (343.7 kB)
picture2.png (236.5 kB)
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 CodeMonkeyYT · May 03 at 05:45 AM

Hmm I'm not sure I understand your question but you can place a Game Object at the end of the gun, use that as the Ray starting point and the center of the screen as the final point.

You can also do a Raycast from the Camera to the Gun to see if the gun is past a wall and if so don't shoot.

I made a Third Person Shooter Controller here https://unitycodemonkey.com/video.php?v=FbM4CkqtOuA

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 Andrea90 · May 04 at 03:43 PM

Hi CodeMonkey ! I wanna say thank you for your time to reply me and thank you for your great work sharing tutorials. I'm one of your subscriber on you tube and I actually use your third person shooter video as a starter point. My solution was not so elegant but it works as I wanted. As you said I have placed a game object with a is trigger collider at the end of my muzzle point and with a specific layer mask. Then I shoot a ray from the camera to that game object and use the hit point to shoot another ray which hit point will give me the destination of my bullet should go . Now everything is in between player and camera won't be considered as a bullet target and I don't have bullets going backward

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

190 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

Related Questions

Gun script using Physics.Raycast not working? 2 Answers

Raycasting in a 2D side scrolling shooter 2 Answers

Altering Material Alpha While Raycast is NOT Hitting 0 Answers

Return 3rd Person camera to original position 0 Answers

How to make a simple line of sight in a 2D top down shooter. 3 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