欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

Unity3D實(shí)現(xiàn)射線使物體移動(dòng)

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例為大家分享了Unity3d如何通過射線使物體移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class RayTest : MonoBehaviour {
  //設(shè)置射線在Plane上的目標(biāo)點(diǎn)target
  private Vector3 target;
 // Use this for initialization
 void Start () {
    //初始化目標(biāo)點(diǎn)與自身的點(diǎn)重合
    target = transform.position;
 }
 
 // Update is called once per frame
 void Update () {
    //當(dāng)點(diǎn)擊鼠標(biāo)左鍵的時(shí)候創(chuàng)建一條射線
    if(Input.GetMouseButton(0))
    {
      //定義射線
      Ray m_ray;
      //保存碰撞信息
      RaycastHit m_hit;
      //創(chuàng)建一條從攝像機(jī)發(fā)出經(jīng)過屏幕上的鼠標(biāo)點(diǎn)的一條射線
      m_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      //判斷射線是否碰撞到物體
      if(Physics.Raycast(m_ray,out m_hit))
      {
        //判斷碰撞到的是不是Plane
        if(m_hit.transform.name=="Plane")
        {
          //把目標(biāo)點(diǎn)target設(shè)置為m_hit.point,//并使物體要處于Plane上所以Y軸為0.5f
          target = new Vector3(m_hit.point.x, 0.5f, m_hit.point.z);
 
        }
      }
    }
    Move(target);
 }
  //移動(dòng)方法
  void Move(Vector3 target)
  {
    if (Vector3.Distance(transform.position, target) > 0.1f)
    {
      transform.position = Vector3.Lerp(transform.position, target,Time.deltaTime);
    }
    //如果物體的位置和目標(biāo)點(diǎn)的位置距離小于 0.1時(shí)直接等于目標(biāo)點(diǎn)
    else
      transform.position = target;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C# 中的IComparable和IComparer的使用及區(qū)別

欄    目:C#教程

下一篇:C#使用Consul集群進(jìn)行服務(wù)注冊(cè)與發(fā)現(xiàn)

本文標(biāo)題:Unity3D實(shí)現(xiàn)射線使物體移動(dòng)

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4575.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有