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.

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

        }
    }
}`