Unity 射线案例

Unity 学习笔记汇总
Debug.DrawRay官方API使用文档

1. 前台

在这里插入图片描述

2. 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update

    public Ray myRay;
    public GameObject cube, sphere, cube2;
    float speed = 2f;
    RaycastHit myHit;


    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 moveDir = sphere.transform.position - cube.transform.position;
        myRay = new Ray(cube.transform.position, moveDir);

        //
        Debug.DrawRay(myRay.origin, myRay.direction * 20, Color.red);
        Debug.DrawRay(cube.transform.position, myRay.direction * 20, Color.red);

        //Debug.DrawRay(myRay.GetPoint(2f), myRay.direction, Color.red);

        //cube.transform.Translate(moveDir * Time.deltaTime * speed);
        //cube2.transform.Translate(moveDir * Time.deltaTime * speed);
    }
}

3. 结果

运行后点击Scene,会看到有一条射线
在这里插入图片描述

发布了605 篇原创文章 · 获赞 637 · 访问量 140万+

猜你喜欢

转载自blog.csdn.net/COCO56/article/details/105241884