Blogger Information
Blog 95
fans 0
comment 11
visits 248639
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Unity3d从屏幕坐标发射一条射线
龍__遇见彩虹的博客
Original
1723 people have browsed it
private Camera mainCamera;

void Start()
{   
        //通过名字找到MainCamera物体,从而获取其身上的Camera组件
    //mainCamera = GameObject.Find("MainCamera").GetComponent(Camera)<>;
    
    //通过标签tag为MainCamera获取MainCamera物体  The first enabled camera tagged "MainCamera" (Read Only).
    mainCamera = Camera.main;
}

void Update()
{
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isHit = Physics.Raycast(ray, out hit);
        
        //如果射线碰撞到物体
        if(isHit)
        {
                //do something
                Debug.Log(hit.collider);
        }
}


// Draws a line in the scene view going through a point 200 pixels
// from the lower-left corner of the screen
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour{    Camera cam;
    void Start()
    {
        cam = GetComponent<Camera>();
    }
    void Update()
    {
            Ray ray = cam.ScreenPointToRay(new Vector3(200, 200, 0));
            Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post