Unity实例开发-奔跑的轮胎

我们使用Unity 3D实现一个简单的超车小游戏。

开发环境:Unity5.0.2以上。

运行效果:

玩家必须在有限时间内进入通关大门才能获得胜利,游戏过程中,玩家可以加速,可以左右移动躲避汽车,图中碰到任何一辆车,或者规定的时间进入通关大门。

关键功能及脚本:

控制玩家左右移动:

public class PlayerScript : MonoBehaviour {

    private float speed_L_R = 0.1F; //玩家左右移动的速度;
    private float speed_F = 0.1F;  //玩家向前的速度;
    // Use this for initialization
    void Start () {
    }
	
    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.W) )  //按下A键翔左运动
	    transform.Translate (-Vector3.forward * speed_F);

	if (Input.GetKey (KeyCode.A) )  //按下A键翔左运动
	    transform.Translate (-Vector3.left * speed_L_R);
	if (Input.GetKey (KeyCode.D))  //按下D键向右运动
	    transform.Translate (-Vector3.right * speed_L_R);
    }

}

摄像机与玩家匹配:

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

public class MyCameraScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
		
    }
	
    // Update is called once per frame
    void Update () {
        Transform caTransform;
	//获取玩家的世界坐标值
	caTransform = GameObject.Find ("player").GetComponent<Transform> ();
	//摄像机的x值设为3
	float Camera_X = 3.0f;
	//摄像机的y值设定
	float Camera_Y = caTransform.position.y + 1.5f;
	//摄像机的z值设定
	float Camera_Z = caTransform.position.z - 5.0f;
	//重置摄像机的位置,使摄像机永远跟随玩家运动
	transform.position = new Vector3 (Camera_X, Camera_Y, Camera_Z);
    }
}

当我们一直往左或者一直往右移动玩家时,就会离开摄像机的视角范围。我们编写脚本使之移动到公路边缘时,便

不能继续移动。

if (Input.GetKey (KeyCode.A) && transform.position.x > -0.7)  //按下A键翔左运动
    transform.Translate (-Vector3.left * speed_L_R);
if (Input.GetKey (KeyCode.D) && transform.position.x < 6.8)  //按下D键向右运动
    transform.Translate (-Vector3.right * speed_L_R);

汽车的随机出现,我们这里有两种汽车,需要让这两种车随机分布并出现在场景中,并且可以向前行驶。需要作为刚体并加上盒子碰撞。:

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

public class MyPreScript : MonoBehaviour {
    public int car_
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值