【Unity 3D】学习笔记(二)

本文探讨了Unity 3D中游戏对象运动的本质,通过矩阵变换实现对象运动,并介绍了如何使用多种方法实现物体的抛物线运动。此外,详细阐述了编程实践,包括游戏脚本分析、游戏对象预制、MVC结构的应用,以及Transform的Rotate和RotateAround方法的向量变换实现。
摘要由CSDN通过智能技术生成

简答题


1. 游戏对象运动的本质是什么?

游戏对象的运动过程本质上是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。

public class MoveLeft : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		this.transform.position += Vector3.left * Time.deltaTime;
	}
}

如以上的脚本,它使得游戏对象每秒钟向左移动一个单位。将代码拖放到任意游戏对象上并运行游戏,可以观察到游戏对象左移。这种运动行为的实现实际上是通过改变对象的坐标实现的。

 

2. 请用三种方法以上方法,实现物体的抛物线运动。(如,修改Transform属性,使用向量Vector3的方法…)

实现物体抛物线运动的基本思路是,将物体运动的速度分解为x方向上和y方向上的分量。在x方向上,物体做匀速运动;在y方向上,物体做加速运动。修改相应方向上的坐标即可实现抛物线运动。

修改Transform属性的方法:

public class mv : MonoBehaviour {
    int xspeed, yspeed, g = 1;

    void Start () {
        xspeed = 1;
        yspeed = 0;
    }

    void Update () {
        yspeed += g;
        transform.position += Vector3.right * Time.deltaTime * yspeed;
        transform.position += Vector3.down * Time.deltaTime * yspeed;
    }
}

使用向量Vector的方法:

public class mv : MonoBehaviour {
    int xspeed, yspeed, g = 1;

    void Start () {
        xspeed = 1;
        yspeed = 0;
    }

    void Update () {
        yspeed += g;
        Vector3 v = new Vector3(Time.deltaTime * yspeed, -Time.deltaTime * yspeed, 0);
        transform.position += v;
    }
}

使用Translate函数来改变坐标的方法:

public class mv : MonoBehaviour {
    int xspeed, yspeed, g = 1;

    void Start () {
        xspeed = 1;
        yspeed = 0;
    }

    void Update () {
        yspeed += g;
        Vector3 v = new Vector3(Time.deltaTime * yspeed, -Time.deltaTime * yspeed, 0);
        transform.Translate(v);
    }
}

 

编程实践


阅读以下游戏脚本:

Priests and Devils

Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!

程序需要满足的要求:

  • play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值