raycasting Ray projection

Source: Internet
Author: User

Game developers can use ray casting for actions like aiming, identifying lines of sight, and measuring distances. There are many overloads of raycast in unity. Now show the most commonly used methods in 2

BOOL Raycast (Vector3 origin, Vector3 direction, float distance, layermask mask);

Parameter interpretation
Origin: The starting position of the light
Direction: direction of Light
Distance: The distance the light travels (can be null)
Mask: determines which layer the light will hit (nullable)


If you want to determine if there is an object before the camera, you can use the following code

void Update () {if (Physics.raycast (Transform.position, Transform.forward, ten)) {print ("There is something in Front of the camera! ");}

Note This code is placed in the camera


Another

BOOL Raycast (Vector3 origin, Vector3 direction, out Raycasthit hits, float distance);

This overloaded method uses a parameter of type Raycasthit, which is the object to which the light is colliding

void Update () {Float DirX = Input.getaxis ("Mouse X");    float DirY = Input.getaxis ("Mouse Y"); Transform.    Rotate (DirY,-dirx, 0); Uptateraycasthit ();}    void Updateraycasthit () {Raycasthit hit; if (Physics.raycast (Transform.position, Transform.forward, out hit)) {Distroy (Hit.collider.gameObject)}}


This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1701717

raycasting Ray projection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.