Unity3D實(shí)現(xiàn)旋鈕控制燈光效果
本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)旋鈕控制燈光效果的具體代碼,供大家參考,具體內(nèi)容如下
前言
實(shí)際上使用的是非常簡(jiǎn)單的方式,通過開啟以及關(guān)閉帶有燈光效果物體的渲染以模擬出的燈光切換效果。
正確方式應(yīng)當(dāng)為物體切換不同的Material實(shí)現(xiàn)效果。
所用函數(shù)
public void RotateAround(Vector3 point, Vector3 axis, float angle);
//通過給定一個(gè)世界坐標(biāo)、軸向以及一個(gè)角度,使物體以該角度旋轉(zhuǎn)繞世界坐標(biāo)點(diǎn)的軸向的變換
public T GetComponent<T>();
//獲取對(duì)象的組件
public bool enabled { get; set; }
//設(shè)置激活狀態(tài)
實(shí)現(xiàn)代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateControl : MonoBehaviour {
public GameObject RedLight;//紅燈
public GameObject GreenLight;//綠燈
public GameObject Center;//旋轉(zhuǎn)中心,若為自身則指定自身
private bool isOn = false;//正在開啟
private bool isOff = false;//正在關(guān)閉
[Range(0,180)]
public float onLine = 80;//旋鈕最大角度
[Range(0,180)]
private float offLine = 0;//旋鈕最小角度
[Range(0,3)]
public float speed = 1;//旋轉(zhuǎn)速度
[Range(0, 20)]
public float LightingRange = 10;//亮燈角度與旋鈕最大角度的角度差
// Use this for initialization
void Start () {
isOn = false;
isOff = false;
offLine = Center.transform.rotation.z;//設(shè)定起始位置即為最小角度
RedLight.GetComponent<Renderer>().enabled = false;//關(guān)閉紅燈渲染
GreenLight.GetComponent<Renderer>().enabled = false;//關(guān)閉綠燈渲染
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R))//關(guān)閉
{
isOn = false;
isOff = true;
}
if (Input.GetKeyDown(KeyCode.G))//開啟
{
isOn = true;
isOff = false;
}
if (isOn == true)
{
if (this.transform.eulerAngles.z < onLine)//旋鈕旋轉(zhuǎn)至最大角度
{
this.transform.RotateAround(this.transform.position, this.transform.forward, speed);
}
else
{
isOn = false;
}
}
if (isOff == true)
{
if (this.transform.eulerAngles.z > offLine + 1)//旋轉(zhuǎn)至最小角度+1°的角度,當(dāng)物體旋轉(zhuǎn)到0時(shí)繼續(xù)旋轉(zhuǎn)則變?yōu)?60度
{
this.transform.RotateAround(this.transform.position, this.transform.forward, -speed);
}
else
{
isOff = false;
}
}
//檢測(cè)旋轉(zhuǎn)角度控制燈光
if (this.transform.eulerAngles.z >= onLine - LightingRange)//當(dāng)旋鈕旋轉(zhuǎn)角度大于閾值則渲染綠燈,關(guān)閉紅燈
{
RedLight.GetComponent<Renderer>().enabled = false;
GreenLight.GetComponent<Renderer>().enabled = true;
}
else
{
RedLight.GetComponent<Renderer>().enabled = true;
GreenLight.GetComponent<Renderer>().enabled = false;
}
}
}
關(guān)于物體的旋轉(zhuǎn)
由于很少情況會(huì)在物體剛好旋轉(zhuǎn)到我們所期望的角度時(shí)進(jìn)行一次判定,所以物體旋轉(zhuǎn)角度一般會(huì)超過你所期望的角度。
當(dāng)物體由一個(gè)正向的角度向0°方向旋轉(zhuǎn)時(shí),物體角度低于0°時(shí)物體角度會(huì)變?yōu)?60°
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#讀取數(shù)據(jù)庫返回泛型集合詳解(DataSetToList)
本篇文章主要是對(duì)C#讀取數(shù)據(jù)庫返回泛型集合(DataSetToList)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
詳解WPF如何在基礎(chǔ)控件上顯示Loading等待動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了WPF如何在基礎(chǔ)控件上顯示Loading等待動(dòng)畫的效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-04-04
C#數(shù)值轉(zhuǎn)換-顯式數(shù)值轉(zhuǎn)換表(參考)
就是在將一種類型轉(zhuǎn)換成另外一種類型時(shí),需要額外的代碼來完成這種轉(zhuǎn)換。2013-04-04
C#使用Zxing.dll組件解析二維碼的實(shí)現(xiàn)
ZXing是一個(gè)開源的,支持多種格式的條形碼圖像處理庫,本文主要介紹了C#使用Zxing.dll組件解析二維碼的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

