//改变刚体速度控制移动
float tempy = rigidbody.velocity.y;
Vector3 tempv = transform.forward * Input.GetAxis("Vertical") * movespeed;
tempv += transform.right * Input.GetAxis("Horizontal") * movespeed;
tempv.y = tempy;
rigidbody.velocity = tempv;
使用Transform.translate()的方式总是丧心病狂的穿透mesh collider,用上述方式可以实现玩家与mesh collider的碰撞
//the bullet is not enough
if (zidan[index] <= 0)
{
return;
}
//fire cooldown
if (Time.time - firetime < jiange[index])
{
return;
}
if (Input.GetMouseButtonDown(0)) {
zidan[index]--;//bullet subs
GameObject huohua = Instantiate(huohuaobj) as GameObject;//copy the fire effect
huohua.transform.parent = shouqianghuohua.transform;
huohua.transform.localPosition = Vector3.zero;
huohua.transform.localEulerAngles = Vector3.zero;
huohua.transform.localScale = new Vector3(1f, 1f, 0);
huohua.SetActive(true);
Destroy(huohua, 0.5f);
RaycastHit hit;
if (Physics.Raycast(ca.transform.position, (Camera.main.ViewportToWorldPoint(miaozhun.transform.position) - shouqianghuohua.transform.position).normalized, out hit, 500/*,1<<8*/))
{
Debug.Log(hit.transform.name);
if (hit.transform.tag == "Enemy")
{
Debug.Log("enemybehit");
//GameObject.Find("Enemy").GetComponent<Enemy>().enemybehit(100);
hit.transform.SendMessage("behit", 100);
firetime = Time.time;
}
}
}
手枪攻击
if (zidan[index] <= 0)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
zidan[index]--;
RaycastHit hit0;
if (Physics.Raycast(ca.transform.position, (Camera.main.ViewportToWorldPoint(miaozhun.transform.position) - shouqianghuohua.transform.position).normalized, out hit0, 500/*,1<<8*/)) {
arrow.SetActive(true);
arrow.transform.position = new Vector3(hit0.point.x,hit0.point.y+0.64f,hit0.point.z);
GameObject planobj = Instantiate(plane,new Vector3(0,10,0),this.transform.rotation) as GameObject;//copy the plane
planobj.SetActive(true);
planobj.transform.LookAt(hit0.point + new Vector3(0, 10, 0));
planeclone = planobj;//赋值给全局变量
StartCoroutine("MovePlane", hit0.point + new Vector3(0, 10, 0));//协同
}
}
IEnumerator MovePlane (Vector3 target) {
while (true) {
planeclone.rigidbody.velocity = planeclone.transform.forward * 10;
if (Vector3.Distance(planeclone.transform.position,target)<10) {
//Debug.Log("kuaishengzhadan");
GameObject rocketobj = Instantiate(Bullet,planeclone.transform.position,planeclone.transform.rotation) as GameObject;
rocketobj.SetActive(true);
rocketobj.transform.localScale = new Vector3(3,3,3);
}
yield return new WaitForSeconds(0.3f);
}