粒子碰撞檢測(cè)
void OnParticleCollision(GameObject? other){
}
拖尾渲染
using Unity Engine;
usingSystem.Collections;
publicclassPlayerScript:MonoBehaviour{
private? Animator? animator;
int? attack;
private TrailRenderer? trailRender;
voidStart( ){
animator=GetComponent<Animator>( );
attack=Animator.StringToHash("Attack");
trailRender=GetComponentInChildren<TrailRenderer>( );//獲取子物體的組件
}
voidUpdate( ){
if(Input.GetMouseButtonDown(0)){
animator.SetTrigger(attack);
}
}
void TrailActive( ){
trailRender.enabled=true;//開(kāi)始攻擊時(shí),渲染開(kāi)始
}
void TrailNoActive( ){
trailRender.enabled=false;//攻擊結(jié)束時(shí),渲染結(jié)束
}
}
線(xiàn)性渲染
lineRenderer.SetVertexCount(2);//先設(shè)置定點(diǎn)數(shù)量(方法)
(lineRenderer.positionCount=2; )(新版本屬性)
lineRenderer.SetPosition(0,transform.position);//設(shè)置第一個(gè)定點(diǎn)位置
linrRenderer.SetPosition(1,targetTransform.position);//設(shè)置第二個(gè)定點(diǎn)位置
導(dǎo)航線(xiàn)性渲染結(jié)合(lineRenderer組件要掛載在第一個(gè)定點(diǎn)上)
usingUnityEngine;
usingSystem.Collections;
publicclassLineRendererScript:MonoBehaviour{
private NavMeshAgent agent;
private LineRenderer line;
public? Transform? startPoint;
public? Transform? endPoint;
voidStart( ){
agent=GetComponent<NavMeshAgent>( );
line=startPoint.gameObject.GetComponent<LineRenderer>( );
}
voidUpdate(){
if(line&&agent){
//生成一個(gè)導(dǎo)航網(wǎng)格的路徑的變量,用來(lái)存儲(chǔ)導(dǎo)航路徑的相關(guān)信息
NavMeshPath?? path=new? NavMeshPath( );
//計(jì)算路徑,得到路徑的相關(guān)信息,并且存儲(chǔ)到path中
agent.CalculatePath(endPoint.position,path);
//根據(jù)path中的拐點(diǎn)來(lái)設(shè)置lineRenderer的相關(guān)屬性
line.SetWidth(0.5f,0.5f);
line.SetColors(Color.red,Color.green);
//1.根據(jù)拐點(diǎn)數(shù)來(lái)設(shè)置頂點(diǎn)數(shù)
line.SetVertexCount(path.corners.Length+2);
//2.設(shè)置開(kāi)始點(diǎn)和結(jié)束點(diǎn)
line.SetPosition(0,startPoint.position);
line.SetPosition(path.corners.Length+1,endPoint.position);
//設(shè)置其他頂點(diǎn)
for(inti=1;i<=path.corners.Length;++i){
line.SetPosition(i,path.corners[i-1]);
}
}
}
}
畫(huà)線(xiàn)(掛載在起點(diǎn)物體)
usingUnityEngine;
usingSystem.Collections;
publicclassDrawLine:MonoBehaviour{
LineRenderer line;
//當(dāng)前頂點(diǎn)下標(biāo)
int index=1;
//頂點(diǎn)數(shù)
int countOfLine=1;
RaycastHit hit;
voidStart(){
line=GetComponent<LineRenderer>( );
line.SetVertexCount(1);
line.SetPosition(0,transform.position);
}
voidUpdate(){
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),outhit)){
if(hit.collider.name=="Plane"){
countOfLine++;
line.SetVertexCount(countOfLine);
if(index<countOfLine){
line.SetPosition(index,hit.point);
index++;
}
}
}
}
}
}