這次的任務(wù)是實(shí)現(xiàn)物理學(xué)運(yùn)動,結(jié)果我發(fā)現(xiàn)在上次作業(yè)中我全給實(shí)現(xiàn)了。
所以這次只要簡單實(shí)現(xiàn)adapter就行了。
model腳本:github
原版代碼:上次作業(yè)
我的adapter是實(shí)現(xiàn)物理學(xué)跟運(yùn)動學(xué)兩種運(yùn)動的接口。
1.原版運(yùn)動public void setdisk(int sp,System.Random r){//改變飛碟速度
int rp = r.Next()%position.GetLength(0);
disk.transform.position = position[rp];
Rigidbody rigidbody;
rigidbody = disk.GetComponent();
//啟動剛體
rigidbody.WakeUp();
rigidbody.useGravity = true;
//添加瞬間力
float disksp = 1;
for(int i = 1;i
2.使用adapterpublic void setdisk(int sp,System.Random r){//改變飛碟速度
int rp = r.Next()%position.GetLength(0);
disk.transform.position = position[rp];
Rigidbody rigidbody;
rigidbody = disk.GetComponent();
//啟動剛體
rigidbody.WakeUp();
float disksp = 1;
for(int i = 1;i
adapter類public class Adapter: System.Object{public Kinematicsn kinematicsn;
public Physis physis;
GameObject disk;
public Adapter(GameObject disk){this.disk = disk;
kinematicsn = disk.AddComponent() as Kinematicsn;
physis = disk.AddComponent() as Physis;
kinematicsn.setdisk(disk);
physis.setdisk(disk);
}
public void move(int mode,Vector3 force,Vector3 rota){if(mode == 1){physis.move(force,rota);
}
else kinematicsn.move(force);
}
public void stop(){kinematicsn.stop();
}
}
物理學(xué)類public class Physis:MonoBehaviour{GameObject disk;
public void setdisk(GameObject disk){this.disk = disk;
}
public void move(Vector3 force,Vector3 rota){Rigidbody rigidbody;
rigidbody = disk.GetComponent();
rigidbody.useGravity = true;
//添加瞬間力
rigidbody.AddForce(force*Random.Range(5, 8)/5, ForceMode.Impulse);
//添加旋轉(zhuǎn)力
rigidbody.AddTorque(rota * 10);
}
}
運(yùn)動學(xué)類public class Kinematicsn:MonoBehaviour{GameObject disk;
Vector3 speed;
float gra = -10.0f;
int work = 0;
public void setdisk(GameObject disk){this.disk = disk;
}
public void move(Vector3 force){speed = force;
work = 1;
}
public void stop(){work = 0;
}
void Update(){if(work == 1){disk.transform.position += speed*Time.deltaTime;
speed.y += gra*Time.deltaTime;
}
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧