• 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
1
Question by Ian9921 · Jul 06, 2018 at 02:53 AM · camerajavascriptraycastraycastingcenter

cast a ray to hit an object in the center of the screen

Okay, I need to set up a ray so that when it is cast from the camera it will hit/destroy what is currently in the center of the screen, and nothing else along the way. Currently, I can cast a ray from the camera, and it will destroy what it hits, but it doesn't hit what's at the center of the screen, and instead hits a bunch of other stuff. My code goes as follows:

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var rotSpeed : float = 5.0;
 var latTarget : Transform;
 var latRotSpeed : float = 5.0;
 public var reach : float = 10;
 var currentTarget :Transform;
 var cameraPos : Transform;
 var target : Transform;
 
 
 
     private var moveDirection : Vector3 = Vector3.zero;
 
     function Start() {
 
     }
 
     function Update() {
 
         var hit : RaycastHit;
 
         var fwd : Vector3 = cameraPos.TransformDirection(Vector3.forward);
 
         var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
 
         var controller : CharacterController = GetComponent.<CharacterController>();
         if (controller.isGrounded) {
             // We are grounded, so recalculate
             // move direction directly from axes
 
             moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
         transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X")*rotSpeed);
         transform.RotateAround(latTarget.position, Vector3.left, Input.GetAxis("Mouse Y")*(latRotSpeed));
         //if (transform.eulerAngles.
 
 
         transform.eulerAngles.z = 0;
         // Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
 
 
 
         // Move the controller
         controller.Move(moveDirection * Time.deltaTime);
         if (Physics.Raycast(ray, hit, reach))
         {
             if(Input.GetMouseButton(1)){
                 Destroy(hit.transform.gameObject);
                 Debug.Log("I'm hitting something");
             }
         }
     }


Thank you for your help, 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
1

Answer by BluesyPompanno · Jul 09, 2018 at 04:11 PM

From what I understood is you are trying to create first person game where you shoot with the center of your screen


You should rewrite your - var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));


To this: var ray : Ray = Camera.main.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));


Here is how you can implement it fully.


if (//Here you can put the key/button the player needs to press in order to fire or some other checks for ammo ,etc.. ) {

         // Create a vector at the center of our camera's viewport
         Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));

         // Declare a raycast hit to store information about what our raycast has hit
         RaycastHit hit;

         // Check if our raycast has hit anything
         if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
         {
             // Rest of your code - what to do when raycast hits anything

         }
     }
 }`








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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

214 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

Related Questions

Raycast and ScreenPointToRay 1 Answer

Raycast hits player when looking down 1 Answer

(Raycasting) Changing Position 0 Answers

i don't understand Vector3.forward correctly on camera 1 Answer

Raycast help..... 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