如上圖我們要實(shí)現(xiàn)以上效果材彪,即讓生成的一圈圖是斜著的冠息!
至于怎么生成一圈物體,這里不做過(guò)多解釋荠瘪,詳情請(qǐng)看上一篇:http://www.reibang.com/p/829f990d81b0(傳送門)
1.實(shí)現(xiàn)一圈圖片斜著的效果:
首先夯巷,我們?cè)O(shè)置相機(jī)到合適的位置,然后調(diào)整角度即可哀墓。如圖:
小提示:如果需要在ui或者別的地方上顯示這樣的效果趁餐,但是主攝像機(jī)又要拍攝場(chǎng)景,可以考慮用創(chuàng)建多一個(gè)攝像機(jī)來(lái)渲染篮绰!然后設(shè)置第二個(gè)攝像機(jī)的里面的參數(shù)即可后雷!
不過(guò)需要注意的是,Culling Mask是拍攝選中的Layer的物體吠各,所以需要拍攝的物體也需要設(shè)置對(duì)應(yīng)的Layer(如下圖)臀突。
前面的只是開胃小菜,接下來(lái)贾漏,我們進(jìn)入主題:
需求:實(shí)現(xiàn)一個(gè)轉(zhuǎn)動(dòng)抽獎(jiǎng)的效果候学,按下按鍵后開始轉(zhuǎn)動(dòng),按下停止按鍵后開始減速停下纵散。
具體上代碼吧梳码,感覺不需要過(guò)多解釋了:
不過(guò)需要注意的是隐圾,這個(gè)腳本是掛在要轉(zhuǎn)動(dòng)的物體上,然后圍繞這個(gè)物體生成一圈子物體掰茶!
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 轉(zhuǎn)動(dòng)的狀態(tài)控制
/// </summary>
public enum RotationState
{
None = 0,
Trun,
Stop,
Stoped,
}
public class MyTest : MonoBehaviour
{
//一個(gè)無(wú)關(guān)代碼的注釋:重點(diǎn)暇藏,我生成的物體起點(diǎn)是從靠近屏幕這邊開始的起點(diǎn),具體用的操作是設(shè)置掛載此腳本的Scale的Z為-1
public GameObject go_Prefab; //生成的prefab
public float fRadius = 500; //圓的半徑
private int m_MaxRotNumber = 20;
private float m_IntervalAngle = 18f;//間隔角度
private List<GameObject> IconObj = new List<GameObject>();
private List<RotTran> m_RectTrans = new List<RotTran>();
private List<float> IconObjYpos = new List<float>();
private RotationState enumRotationState = RotationState.None;//當(dāng)前狀態(tài)
float fStopTargetAngle = 0; //停止的目標(biāo)角度
float fAngle = 0; //旋轉(zhuǎn)的角度
public float fRotationSpeed = 200; //旋轉(zhuǎn)速度
float fOriginRotationSpeed = 0; //用來(lái)記錄旋轉(zhuǎn)速度濒蒋,用來(lái)重置數(shù)據(jù)
float fSlowDownRotationSpeed = 50;//最慢旋轉(zhuǎn)速度
private struct RotTran
{
public Vector3 r_Pos;
public Vector3 r_Eul;
public RotTran(Vector3 pos, Vector3 eul)
{
r_Pos = pos;
r_Eul = eul;
}
}
private RotTran NewRot;
// Use this for initialization
void Start()
{
fOriginRotationSpeed = fRotationSpeed;
for (int a = 0; a < m_MaxRotNumber; a++)
{
GameObject newTemp = GameObject.Instantiate(go_Prefab);
newTemp.name = a.ToString();
newTemp.transform.parent = this.transform;
IconObj.Add(newTemp);
//生成位置信息叨咖,圓形的公式
// x = 原點(diǎn)x + 半徑 * 鄰邊除以斜邊的比例, 鄰邊除以斜邊的比例 = cos(弧度) , 弧度 = 角度 *3.14f / 180f(大致的參考公式)
NewRot = new RotTran(new Vector3((Mathf.Sin((m_IntervalAngle * a) / 180 * Mathf.PI) * fRadius), 0, fRadius * Mathf.Cos((m_IntervalAngle * a) / 180 * Mathf.PI)), new Vector3(0, m_IntervalAngle * a, 0));
m_RectTrans.Add(NewRot);
IconObjYpos.Add(NewRot.r_Pos.y);
}
//設(shè)置位置和旋轉(zhuǎn)
for (int i = 0; i < IconObj.Count; i++)
{
//IconObj[i].transform.localScale = Vector3.one;
IconObj[i].transform.localPosition = m_RectTrans[i % IconObj.Count].r_Pos;
IconObj[i].transform.localEulerAngles = m_RectTrans[i % IconObj.Count].r_Eul;
}
}
// Update is called once per frame
void Update()
{
switch (enumRotationState)
{
case RotationState.None:
break;
case RotationState.Trun:
TurnChild();
break;
case RotationState.Stop:
StopRotation();
break;
case RotationState.Stoped:
ReSetAllData();
break;
}
if (Input.GetKeyDown(KeyCode.A) && enumRotationState == RotationState.Trun)
{
SetStop();
}
if (Input.GetKeyDown(KeyCode.S) && enumRotationState == RotationState.None)
{
enumRotationState = RotationState.Trun;
}
}
/// <summary>
/// 旋轉(zhuǎn)
/// </summary>
void TurnChild()
{
fAngle += fRotationSpeed * Time.deltaTime;
transform.rotation = Quaternion.Euler(new Vector3(0, fAngle, 0));
if (IconObj[0].transform.position.x < 0)//<0為順時(shí)針旋轉(zhuǎn),判斷子物體的x軸世界坐標(biāo)是否小于0啊胶,小于0則進(jìn)入處理甸各,
{
GameObject tempGo = IconObj[0];
tempGo.transform.SetSiblingIndex(transform.childCount - 1);//將此物體的層級(jí)設(shè)置到最后一個(gè)
IconObj.RemoveAt(0);
IconObj.Add(tempGo);
}
}
/// <summary>
/// 設(shè)置停止信號(hào)
/// </summary>
void SetStop()
{
fAngle = 0;
fStopTargetAngle = 360 + IconObj[0].transform.localEulerAngles.y;
Debug.Log(IconObj[0].name);
enumRotationState = RotationState.Stop;
}
/// <summary>
/// 停止旋轉(zhuǎn)
/// </summary>
void StopRotation()
{
fAngle += fRotationSpeed * Time.deltaTime;
fRotationSpeed -= 50 * Time.deltaTime;
fRotationSpeed = Mathf.Clamp(fRotationSpeed, fSlowDownRotationSpeed, 200);
if (fAngle >= fStopTargetAngle)
{
fAngle = fStopTargetAngle;
enumRotationState = RotationState.Stoped;
}
transform.rotation = Quaternion.Euler(new Vector3(0, fAngle, 0));
if (IconObj[0].transform.position.x < 0)//<0為順時(shí)針旋轉(zhuǎn)
{
GameObject tempGo = IconObj[0];
tempGo.transform.SetSiblingIndex(transform.childCount - 1);
IconObj.RemoveAt(0);
IconObj.Add(tempGo);
}
}
/// <summary>
/// 重置數(shù)據(jù)
/// </summary>
void ReSetAllData()
{
fAngle = 0;
fStopTargetAngle = 0;
fRotationSpeed = fOriginRotationSpeed;
enumRotationState = RotationState.None;
}
}