Unity3D课程——空间与运动

8 篇文章 1 订阅
7 篇文章 0 订阅

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

游戏对象通过脚本改变其位置,角度和大小


请用三种方法以上方法,实现物体的抛物线运动

修改Transform属性
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
	public float initSpeed = 20f;
	public float angle = 45f;
	public float grity = 10f;
	private float xSpeed, ySpeed;

    // Start is called before the first frame update
    void Start(){
		xSpeed = initSpeed * Mathf.Cos(angle);
		ySpeed = initSpeed * Mathf.Sin(angle);
	}

    // Update is called once per frame
    void Update() {
		Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
		transform.position += mov;
		ySpeed -= grity * Time.deltaTime;
    }
}

使用向量Vector3方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
	public float initSpeed = 20f;
	public float angle = 45f;
	public float grity = 10f;
	private float xSpeed, ySpeed;

    // Start is called before the first frame update
    void Start(){
		xSpeed = initSpeed * Mathf.Cos(angle);
		ySpeed = initSpeed * Mathf.Sin(angle);
	}

    // Update is called once per frame
    void Update() {
		Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
		transform.position = Vector3.Lerp(transform.position, transform.position + mov, 1);
		ySpeed -= grity * Time.deltaTime;
    }
}

使用transform的translate方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
	public float initSpeed = 20f;
	public float angle = 45f;
	public float grity = 10f;
	private float xSpeed, ySpeed;

    // Start is called before the first frame update
    void Start(){
		xSpeed = initSpeed * Mathf.Cos(angle);
		ySpeed = initSpeed * Mathf.Sin(angle);
	}

    // Update is called once per frame
    void Update() {
		Vector3 mov = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
		transform.Translate(mov);
		ySpeed -= grity * Time.deltaTime;
    }
}

写一个程序,实现一个完整的太阳系, 其他星球围绕太阳的转速必须不一样,且不在一个法平面上

步骤
  1. 创建九个sphere,分别命名为sun以及其他八大行星,将八大行星作为sun的子对象
  2. 编写脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour{
	public float RevolutionSpeed = 1f;		//公转速度
	public float RotationSpeed = 30f;       //自转速度
	public Vector3 Axis = Vector3.up;

    void Update(){
		transform.RotateAround(transform.parent.position, Axis, RevolutionSpeed * Time.deltaTime);
		transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
	}
}
  1. 将脚本挂载到八个行星上,在UI界面中分别修改各个行星的公转速度,自转速度,角度等参数即可实现题目要求
  2. 为各对象添加材质包,添加背景以及轨迹
  3. 结果如下图
    在这里插入图片描述

ps. 项目包含在gmae2中的SolarSystem文件夹中,由于材质包过大,删去了一部分,所以最后的效果要次于上图演示的效果


编程实践

  • 阅读以下文本

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!

列出游戏中提及的事物(Objects)

牧师、恶魔、船、河流、两边的河岸

用表格列出玩家动作表(规则表)
动作条件结果
点击人物船在当前人物所在的河岸边;如果人物在岸上,船上的人物要少于两个人物上船/上岸
点击go按钮船靠岸,且船上至少有一个人物船开向对岸
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值