1 創建地板(floor)和四面牆壁,new Plane實現
自帶的網格碰撞器Mesh Collider,勾選Convex,否則碰撞其他物體無效
2 創建幾個cube物體,添加Box Collider和RigidBody,爲cube創建腳本,控制物體移動,由於移動過程中物體會產生反轉,造成前後方向異常,可以採用在updata方法裏重置物體的旋轉角度的方法:transform.localRotation = Quaternion.Euler(0, 0, 0);(四元數重置物體的旋轉角度)
3 控制物體移動:
方法一:改變物體的位移: transform.Translate(Vector3.left * speed);
方法二:爲物體添加一個力:rigidbody.AddForce(Vector3.left * speed);
4 控制物體跳躍:
爲了防止物體跳躍過高,需要提前處理跳躍邏輯,只在地面上可以跳躍,需要使用變量控制
1 爲floor物體添加一個Tag標籤,命名爲Floor,
2 物體的碰撞事件(碰撞器):onCollisionEnter() OnCollisionExit() OnCollisionStay(),由於物體受重力影響,會一隻在floor上面,物體的碰撞只在OnCollisionStay()方法處理,變量的值也在這個方法裏控制
void OnCollisionStay(Collision other)
{
Debug.Log("OnCollisionStay other :"+ other.gameObject.tag);
if(other.gameObject.tag =="Floor")
{
isGround =true;
}
}
Update()方法:
if(Input.GetKey(KeyCode.Space))
{
if(isGround)
{
transform.Translate(Vector3.up );
isGround =false;
}
}
5 控制射線模擬射擊行爲
1 在需要發射射線的位置添加組件:Line Renderer;Light;Particle system
2 Line Renderer 配置材質(射線的顏色),射線起始寬度,結束寬度
3 Light裏面配置點光源(Point),設置光源顏色(決定了射擊時槍口光顏色)
4 射線發射物體創建子物體FaceLight(可選),決定了射擊時正前方的顏色
5 注意:前4個組件默認不選中,以控制射擊動作時射線和光線閃爍
6 編寫代碼,控制射線
1 awake函數裏獲取每個組件對象
2 updata函數裏判斷鼠標左鍵點擊動作,點擊鼠標左鍵即可觸發射擊操作
if(Input.GetButtonDown("Fire1")&& timer >= betweenTwoShot &&Time.timeScale !=0)
{
Shooting();
}
gunLight.enabled =true;
faceLight.enabled =true;
gunParticle.Stop();
gunParticle.Play();
gunLine.enabled =true;
gunLine.SetPosition(0, transform.position);
Debug.Log("射線的位置:"+ transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
shootableMask =LayerMask.GetMask("shooter");
if(Physics.Raycast(shootRay,out shootHit, gunRange, shootableMask))//光線投射(射線起點,射線方向,射線長度,碰撞器)
{
//進入這個方法只是表示射線射擊到了這個layer裏,並不表示射擊到了目標物體
Debug.Log("射線已投射......");
gunLine.SetPosition(1, shootHit.point);
}
// If the raycast didn't hit anything on the shootable layer...
else
{
// ... set the second position of the line renderer to the fullest extent of the gun's range.
gunLine.SetPosition(1, shootRay.origin + shootRay.direction * gunRange);
Debug.Log("else......");
}
6 設置腳本模擬敵人受到傷害
1 爲敵人物體創並掛上腳本CubeHealth,射線發出後,由於敵人屬於環境layer層的子類,可以獲取到射線
2 使用射線碰撞器方法獲取射線碰撞組件是否已經掛CubeHealth腳本的組件,獲取到後做下一步處理
CubeHealth cubeHealth = shootHit.collider.GetComponent<CubeHealth>();
if(cubeHealth !=null)
{
Debug.Log("射線已投射--------------");
}
publicvoidTakeDamage(int hit,Vector3 point)
{
defaultHealth -= hit;
Debug.Log("cube health :"+ defaultHealth);
if(defaultHealth <=0)
{
Destroy(gameObject);
}
}
publicclassCameraFollow:MonoBehaviour
{
publicTransform target;//設置跟隨的對象,這裏寫主角的tag
Vector3 offset;
publicfloat smoothTime =5f;
// Use this for initialization
voidStart()
{
offset = transform.position - target.position;
}
// Update is called once per frame
voidUpdate()
{
Vector3 v3 = target.position + offset;
//插值器,表示在一定時間內(第三個參數),從座標一(第一個參數)移動到座標二(第二個參數)
transform.position =Vector3.Lerp(transform.position, v3, smoothTime *Time.deltaTime);
}
}
publicvoid onButtonClick()
{
canvas.gameObject.SetActive(false);//設置Canvas隱藏
human.gameObject.SetActive(true);//設置主角顯示
environment.gameObject.SetActive(true);//設置環境顯示
}
publicclass cubeManager :MonoBehaviour
{
publicGameObject cubeObject;
publicGameObject sphereObject;
voidStart()
{
InvokeRepeating("CreateCube",3f,3f);
}
voidCreateCube()
{
Vector3 v3 = transform.TransformDirection(Random.Range(0f,6f),Random.Range(0f,4f),Random.Range(-26f,-6f));
Instantiate(cubeObject, v3, transform.rotation);
Instantiate(sphereObject, v3, transform.rotation);
}
}
publicclassDestoryObject:MonoBehaviour{
//設置cube或者floor 的is Trigger爲true時,纔會執行此函數
voidOnTriggerEnter(Collider other)
{
Debug.Log("OnTriggerEnter other :"+ other.gameObject.tag);
if(other.gameObject.tag =="cube")
{
Destroy(other.gameObject);
}
if(other.gameObject.tag =="sphere")
{
Destroy(other.gameObject);
}
}
voidOnTriggerStay(Collider collider)
{
}
voidOnTriggerExit(Collider collider)
{
}
}