Unity入门:第二个游戏改进

这是半成品:

Unity入门:第二个游戏改进_第1张图片

1、加入更多的陨石

如法炮制

2、让背景动起来

duplicateBackground,设置Background01为Background的孩子。

BGScroller:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class BGScroller : MonoBehaviour {

          publicfloat scrollSpeed;

          publicfloat tileSizeZ;

          privateVector3 startPosition;

          //Use this for initialization

          voidStart ()

          {

                       startPosition= transform.position;

          }

          //Update is called once per frame

          voidUpdate ()

          {

                       floatnewPosition = Mathf.Repeat(Time.time * scrollSpeed,tileSizeZ);

                       transform.position= startPosition + Vector3.forward * newPosition;
          }
}

Mathf.repeat是浮点数取余运算,tileSizeZ设置为30,取出来的数值就在0到30之间,然后改变下一个位置,即用当前的位置加上这个新位置。为什么不会移出去(看见黑色)?

因为:

Unity入门:第二个游戏改进_第2张图片

加入了孩子以后,移动范围在0——30,永远不会移出去。类似的,如果不加孩子,坐标在中心,可以把移动范围设置成0——15。

3、加入敌机

这个有一定的难度。首先新建一个GameObject,导入敌机模型,还有喷气粒子系统,设置好EnemyBolt的位置,开火的声音等:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponController : MonoBehaviour {
    private AudioSource audio;
    public GameObject shot;
    public Transform shotSpawn; 
    public float fireRate;
    public float delay;

    // Use this for initialization
    void Start () 
    {
        audio = GetComponent ();
        InvokeRepeating ("Fire",delay,fireRate);
    }

    void Fire()
    {
        Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
        audio.Play ();
    }       
}

敌机的移动:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EvasiveManeuver : MonoBehaviour {

    public Vector2 startWait;
    public float dodge;
    public Vector2 maneuverTime;
    public Vector2 maneuverWait;
    public float smoothing;
    public Boundary boundary;
    public float tilt;
    private float targetManeuver;
    private Rigidbody rb;
    private float currentSpeed;
    void Start () 
    {
        rb = GetComponent ();
        currentSpeed = rb.velocity.z;
        StartCoroutine (Evade());
    }

    IEnumerator Evade()
    {
        yield return new WaitForSeconds (Random.Range(startWait.x,startWait.y));
        while (true) 
        {
            targetManeuver = Random.Range (1,dodge)*-Mathf.Sign(transform.position.x);
            yield return new WaitForSeconds (Random.Range(maneuverTime.x,maneuverTime.y));
            targetManeuver = 0;
            yield return new WaitForSeconds (Random.Range(maneuverWait.x,maneuverWait.y));
        }
    }
        
    void FixedUpdate () 
    {
        float newManeuver=Mathf.MoveTowards (rb.velocity.x,targetManeuver,Time.deltaTime*smoothing);
        rb.velocity=new Vector3(newManeuver,0.0f,currentSpeed);
        rb.position=new Vector3(
            Mathf.Clamp(rb.position.x,boundary.xMin,boundary.xMax),
            0.0f,
            Mathf.Clamp(rb.position.z,boundary.zMin,boundary.zMax));
        rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * tilt);
    }
}

startWait:敌机出现的间隔

maneuver应该是表示...挪一挪。Mathf.sign总是让敌机向反方向dodge:如果在x<0的区域,就往x>0的区域走,反之亦然。

maneuverTime是dodge的时间,maneuverWait是下一次dodge的间隔。

MoveTowards(a,b,c)的作用是让a以c的帧率变化到b,这里用来改变x方向的速度值。

成品:

Unity入门:第二个游戏改进_第3张图片



你可能感兴趣的:(Unity)