Unity入门:第二个游戏改进

这是半成品:


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之间,然后改变下一个位置,即用当前的位置加上这个新位置。为什么不会移出去(看见黑色)?

因为:


加入了孩子以后,移动范围在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<AudioSource> ();
        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<Rigidbody> ();
        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方向的速度值。

成品:




  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值