生存射手学习笔记

来源:互联网 发布:数组 字符串 转换 js 编辑:程序博客网 时间:2024/05/02 01:54
主角控制

  *************** *************     移动    ****************************


public class playmove : MonoBehaviour { 
 public float speed = 1; 
void Start () {
         rigi = this.GetComponent<Rigidbody> ();  先要得到组件,所以要先声明  private Rigidbody rigi;

void Update () { 
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical"); 
// transform.Translate (new Vector3 (h, 0, v) * speed * Time.deltaTime);  //移动的方向,这样不会在乎Collider,只在乎位置 
      rigi.MovePosition (transform.position + new Vector3(h,0,v)*speed*Time.deltaTime); //当前的位置加上移动的方向
}

*************** ***********        相机跟随        ************ ********* 

 1. 利用标签来,把主角标签设置为“Player”
 
     public class Tags  { 
              public const string play = "Player"; 
         } 
   public class FollowTarget : MonoBehaviour { 
private Transform player; 
public float smoothing = 3;
    void Start () {
              player = GameObject.FindGameObjectWithTag (Tags.play).transform; //得到主角
        } 
void Update () { 
Vector3 targetPos = player.position + new Vector3 (0, 6, -6);//主角位置加位置差(相机的相对于位置) 
transform.position = Vector3.Lerp(transform.position,targetPos,smoothing*Time.deltaTime); 
}

************ **********        人物转向        ******** **************************
 
 1. 利用鼠标点击与地面形成射线,来控制。先把地面设置为一个单独的层。 
         private int groundLayerIndex = -1;  
          groundLayerIndex = LayerMask.GetMask ("Ground");  
             //控制转向
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
         RaycastHit hitInfo; 
         if (Physics.Raycast (ray, out hitInfo, 200, groundLayerIndex))

               Vector3 target = hitInfo.point;
             target.y = transform.position.y;
               transform.LookAt (target);

2、 //控制动画
        if (h != 0 || v != 0) {
            anim.SetBool("Move", true);
        } else {
            anim.SetBool("Move", false);
        }


*********************   控制射击    **************************

void Update () {
        timer+=Time.deltaTime;
        if (timer > 1 / shootRate) {
            timer -= 1 / shootRate;
            Shoot();
        }
}
    void Shoot() {
        light.enabled = true;
        particleSystem.Play();
        this.lineRenderer.enabled = true;
        lineRenderer.SetPosition(0, transform.position);
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo)) {

            lineRenderer.SetPosition(1, hitInfo.point);
                                                                                          //判断当前的射击有没有碰撞到敌人
            if (hitInfo.collider.tag == Tags.enemy) {
                hitInfo.collider.GetComponent<EnemyHealth>().TakeDamage(attack,hitInfo.point);
            }


        } else {
            lineRenderer.SetPosition(1, transform.position + transform.forward * 100);
        }
        audio.Play();


        Invoke("ClearEffect", 0.05f);
    }



战斗场景

 

1. 设置敌人跟随主角,需要用到导航组件。“windows--navigation”,再点击“Back”按钮烘焙一下。则整个绿色的部分就是导航的部分。


     也就是利用导航网格来追击主角的运动, 给兔子添加一个“Nav Mesh Agent” 组件。
 
2.  再添加一个脚本“Enemy Move”。


*******************************************         追击主角          ********************************************
  
public class EnemyMove : MonoBehaviour { 
private NavMeshAgent agent; 
private Transform player; 
void Awake () { 
agent = this.GetComponent<NavMeshAgent> (); 

}  
void Start () { 
player = GameObject.FindGameObjectWithTag (Tags.play).transform; 

void Update () {
        if (Vector3.Distance(transform.position, player.position) < 1.5f) {
            agent.Stop();
            anim.SetBool("Move", false);
        } else {
            agent.SetDestination(player.position);   //
            anim.SetBool("Move", true);
        }
 
********** *************         敌人血值         ********************************************


如果在敌人身上挂载两个音乐? 
     private AudioSource audio; //受到伤害的声音 
     audio = this.GetComponent<AudioSource> (); 
     audio.Play (); 
      public AudioClip deathClip;  //死亡时声音,使用静态方法  

     AudioSource.PlayClipAtPoint (deathClip, transform.position, 1.0f);
 
*******************************************         自动生成敌人         ********************************************
 
 1. 创建一个空的游戏物体,reset一下。命名为Spawn。 
 2. 在其下创建几个空的子物体,分别命名为三个敌人的名称。分别把位置拉倒生成的地方。分别设置标签 
 3.  
        public class Spawn : MonoBehaviour { 
public GameObject enemyPrefab;
public float spawntime = 3;
private float time = 0;
 
          void Update () { 
time += Time.deltaTime;  //计时
  if (time >= spawntime) {  //如果到达生成时间
     time -= spawntime;
     SpawnEnemy ();
}
}
void SpawnEnemy(){   //生成语句,不进行旋转 
GameObject.Instantiate(enemyPrefab, transform.position, transform.rotation); 
}
}
 
 难度增加:
 
  void Start(){
     InvokeRepeating("ACC", 1, 1);
}
 
  void ACC(){
   spawntime -= 0.05f;   //每隔1s就把生成敌人的时间减少
}


0 0