unity中C#跟随和射线

using UnityEngine;
using System.Collections;
public class gensui : MonoBehaviour {
    public Transform a; //a是主物体被跟随物体
    public Transform b; //b是跟随主物体的物体
    // public Animator ani;
    public NavMeshAgent agint; //是跟随主物体的物体,获取组件
    public LineRenderer lr; //射线渲染
    // Use this for initialization
    void Start () {
       
        //射线长度
        lr.SetVertexCount(2);
        //跟随移动
        /*
        1.选中plane--选择属性列表里的Static
        2.选中plane--Windows--Navigation--右下角Back烘焙一下
        3.建白Cube,建黑Cube
        */
    }
    // Update is called once per frame
    void Update () {
        //跟随
        agint.SetDestination(a.position);
        //射线的两种方法
        1.1.屏幕发出,是摄像机射出的绿色线
        实例化射线
        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        检测射线碰撞
        //RaycastHit hit;
        //if (Physics.Raycast(ray,out hit))
        //{
        //    Debug.Log(hit.point);
        //}
        //Debug.DrawLine(ray.origin,hit.point,Color.green);
        2.指定射线,是被跟随物体射出来的线
        //Ray ray2 = new Ray(a.position,b.position-a.position);
        //RaycastHit hit2;
        //if (Physics.Raycast(ray2,out hit2))
        //{
        //    Debug.Log(hit2.point);
        //    Debug.Log(hit2.collider.gameObject.name);
        //}
        画射线
        //Debug.DrawLine(ray2.origin, ray2.direction*20, Color.red);
        //Move();
        look();
        Fire();
    }
    void look() {
        Ray ray = Camera .main.ScreenPointToRay( Input .mousePosition);
  //射线命中检测
        RaycastHit hit;
//物理方法检测射线
        if ( Physics .Raycast(ray, out hit))
        {
            //射线朝向跟随鼠标
            a.LookAt( new Vector3 (hit.point.x,0.1f,hit.point.z));
        }
    }
    void Fire() {
        Ray ray2 = new Ray (a.position, a.forward);
  //射线命中检测
        RaycastHit hit2;
        if ( Input .GetMouseButtonDown(0))
        {
            //再次点击出射线
            lr.enabled = true ;
//传射线坐标  起始点位置靠上
            lr.SetPosition(0, new Vector3 (A.position.x,1,A.position.z) );
                 //             终点朝向,玩家位置
            lr.SetPosition(1,a.position+a.forward*15);
  //开启时间间隔协程
            StartCoroutine( "startTime" );
            if ( Physics .Raycast(ray2, out hit2))
            {
                Destroy(hit2.collider.gameObject);
            }
        }
    }
    IEnumerator startTime() {
        yield return new WaitForSeconds (0.1f);
        lr.enabled = false ;
    }

}

做记录用,不喜勿喷!!!谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Batman1208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值