牧师与恶魔

这篇博客介绍了在Unity中实现游戏对象的抛物线运动,通过四种不同的方法展示了如何让物体沿着抛物线轨迹移动。此外,还详细讲解了如何构建一个完整的太阳系模型,各个行星以不同的转速和不在同一平面内围绕太阳旋转,提供了一段完整的Unity脚本来实现这一过程。这对于理解物理运动规律和Unity游戏开发具有很好的实践指导意义。
摘要由CSDN通过智能技术生成

1.简答并用程序验证

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

游戏对象运动的本质游戏对象随着每一帧的变化,所处空间的变化,即空间坐标的变换。(包括position和rotation的变化,对应绝对位置或相对位置的变化和旋转角度的变化)

用三种以上方法实现物体的抛物线

方法一:修改transform,直接对position叠加

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class transform : MonoBehaviour {
 	//初始化x方向速度1,g为9.8
    public float vx = 1;
	public float vy = 0;
	public float g = 9.8;
	void Start () {
        
	}
	
	// Update is called once per frame
	void Update () {
 		vy+=g*Time.deltaTime;
        this.transform.position += Vector3.down * Time.deltaTime * vy;
        this.transform.position += Vector3.right * Time.deltaTime * vx;
        
	}
}

方法二:创建一个Vector对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Vector : MonoBehaviour {
 	//初始化x方向速度1,g为9.8
    public float vx = 1;
	public float vy = 0;
	public float g = 9.8;
	void Start () {
        
	}
	
	// Update is called once per frame
	void Update () {
 		vy+=g*Time.deltaTime;
 		Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
 		this.transform.position += v;
        
	}
}

方法三:使用transform.Translate方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Vector : MonoBehaviour {
 	//初始化x方向速度1,g为9.8
    public float vx = 1;
	public float vy = 0;
	public float g = 9.8;
	void Start () {
        
	}
	
	// Update is called once per frame
	void Update () {
 		vy+=g*Time.deltaTime;
 		Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
 		this.transform.translate(v);
        
	}
}

方法四:使用Vector3中的Lerp

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Vector : MonoBehaviour {
 	//初始化x方向速度1,g为9.8
    public float vx = 1;
	public float vy = 0;
	public float g = 9.8;
	void Start () {
        
	}
	
	// Update is called once per frame
	void Update () {
 		vy+=g*Time.deltaTime;
 		Vector3 v = new Vector3(vx*Time.deltaTime,-vy*Time.deltaTime,0);
 		this.transform.translate(v);
        this.transform.position = Vector3.Lerp (transform.position, transform.position + v, 1);
	}
}
实现一个完整的太阳系,其他星球围绕太阳转速不一致,且不在一个法平面上

1.新建一个Sphere命名为Sun,以Sun为父对象,创建八个行星,从网络上寻找贴图加入到Assets中,拖到球上。
在这里插入图片描述
2.编写脚本

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

public class SolarSystem : MonoBehaviour
{
    //用于和各行星对应
    public Transform Sun;  
    public Transform Earth;  
    public Transform Mars;  
    public Transform Mercury;  
    public Transform Venus;  
    public Transform Jupiter;  
    public Transform Saturn;  
    public Transform Uranus;  
    public Transform Neptune; 

    // Start is called before the first frame update
    void Start()
    {
        //初始化位置
        Sun.position = new Vector3(0, 0, 0);
        Earth.position = new Vector3(-6, 0, 0);
        Mars.position = new Vector3(9, 0, 0);
        Mercury.position = new Vector3(3, 0, 0);
        Venus.position = new Vector3(5, 0, 0);
        Jupiter.position = new Vector3(-11, 0, 0);
        Saturn.position = new Vector3(14, 0, 0);
        Uranus.position = new Vector3(-20, 0, 0);
        Neptune.position = new Vector3(22, 0, 0);

    }

    // Update is called once per frame
    void Update()
    {
        //public void RotateAround(Vector3 point, Vector3 axis, float angle);//公转
        //Rotate函数表示自转
        
        Earth.RotateAround(Sun.position, Vector3.up, 14 * Time.deltaTime);
        Earth.Rotate(Vector3.up * 34 * Time.deltaTime);
        
	    Mars.RotateAround(Sun.position, new Vector3(0, 14, 5), 12 * Time.deltaTime);
        Mars.Rotate(new Vector3(0, 13, 6) * 45 * Time.deltaTime);
        
        Mercury.RotateAround(Sun.position, new Vector3(0, 4, 1), 16 * Time.deltaTime);
        Mercury.Rotate(new Vector3(0, 5, 1) * 5 * Time.deltaTime);
        
	    Venus.RotateAround(Sun.position, new Vector3(0, 2, 1), 17 * Time.deltaTime);
        Venus.Rotate(new Vector3(0, 2, 1) * Time.deltaTime);
        
	    Jupiter.RotateAround(Sun.position, new Vector3(0, 9, 4), 11 * Time.deltaTime);
        Jupiter.Rotate(new Vector3(0, 10, 3) * 32 * Time.deltaTime);
	    
	    Saturn.RotateAround(Sun.position, new Vector3(0, 2, 1), 10 * Time.deltaTime);
        Saturn.Rotate(new Vector3(0, 3, 1) * 21 * Time.deltaTime);
        
        Uranus.RotateAround(Sun.position, new Vector3(0, 10, 1), 8 * Time.deltaTime);
        Uranus.Rotate(new Vector3(0, 10, 1) * 23 * Time.deltaTime);
        
        Neptune.RotateAround(Sun.position, new Vector3(0, 8, 2), 6 * Time.deltaTime);
        Neptune.Rotate(new Vector3(0, 9, 1) * 30 * Time.deltaTime);

    }
}

3.新建一个空对象(GameObject),把脚本挂载到GameObject上,然后将各个行星拖入
在这里插入图片描述
4.最后效果如下
在这里插入图片描述

2.编程实践

阅读以下脚本

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)

牧师(Priest)、魔鬼(Devil)、船(Boat)、河流(River)、左右河岸(Side)。

用表格列出玩家动作表(规则表)
动作结果
点击go船移到对岸
点击角色牧师死亡/角色移动/游戏胜利
将游戏中对象做成预制

在这里插入图片描述
在这里插入图片描述

在场景控制器 LoadResources 方法中加载并初始化 长方形、正方形、球 及其色彩代表游戏中的对象
//加载场景
	public void LoadResources()
    {
        left_side = new Side("left");
        right_side = new Side("right");
        boat = new Boat();
        role_array = new List<Role>();
        river = Instantiate(Resources.Load("Prefabs/Water",typeof(GameObject)),new Vector3(0, -0.5F, -0.49F), Quaternion.identity) as GameObject;
        
        for (int i = 0; i < 3; i++)
        {
            Role role = new Role("priest");
            role.SetName("priest" + i);
            role.SetPosition(left_side.GetEmptyPosition());
            role.GoSide(left_side);
            left_side.AddRole(role);
            role_array.Add(role);
        }
        for (int i = 0; i < 3; i++)
        {
            Role role = new Role("devil");
            role.SetName("devil" + i);
            role.SetPosition(left_side.GetEmptyPosition());
            role.GoSide(left_side);
            left_side.AddRole(role);
            role_array.Add(role);
        }
    }
整个游戏仅 主摄像机 和 一个 Empty 对象在这里插入图片描述

FirstController挂载在空对象GameObject上

MVC架构

分别为模型(Model)、控制器(Controller)和界面(View)在这里插入图片描述

最终效果

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值