游戏对象运动的本质是游戏对象位置和状态的改变。通过游戏对象transform属性的position、rotation和scale等属性的变化来实现运动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class methodA : MonoBehaviour {
public int Myspeed = 2;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.right * Time.deltaTime;
this.transform.position += Vector3.down * Time.deltaTime * Time.deltaTime * Myspeed;
}
}
static function MoveTowards (current : Vector3, target : Vector3, maxDistanceDelta : float) : Vector3
物体每一帧都在水平方向和竖直方向移动,与方法1同理。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class methodB : MonoBehaviour {
Vector3 target1 = Vector3.right * 5;
Vector3 target2 = Vector3.down * 5;
float speed1 = 1;
float speed2 = 2;
// Update is called once per frame
void Update(){
float step1 = speed1 * Time.deltaTime;
float step2 = speed2 * Time.deltaTime * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target1, step1);
transform.position = Vector3.MoveTowards(transform.position, target2, step2);
}
}
static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3
按照数字t在from到to之间插值。t大小在 [0…1]之间,当t = 0时,返回from,当t = 1时,返回to。当t = 0.5 返回from和to的平均数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class methodC : MonoBehaviour {
public float Speed = 0.5f;
Vector3 Target1 = new Vector3(-6, -3, 8);
//控制物体向Target移动
void Update()
{
gameObject.transform.localPosition = Vector3.Lerp(transform.position, Target1, Speed * Time.deltaTime);
}
}
public void Rotate(Vector3 eulers,Space relativeTo)
public void RotateAround(Vector3 point,Vector3 axis,float angle)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tianwang : MonoBehaviour {
public Transform Sun;
public float rotationSpeed = 2;
public float speed = 8.5f;
private float rx;
private float ry;
private float rz;
// Use this for initialization
void Start()
{
rx = Random.Range(10, 30);
ry = Random.Range(40, 60);
rz = Random.Range(10, 30);
}
// Update is called once per frame
void Update()
{
Vector3 axis = new Vector3(rx, ry, rz);
this.transform.RotateAround(Sun.position, axis, speed * Time.deltaTime);
transform.Rotate(Vector3.down * rotationSpeed, Space.World);
}
}
(3)设置摄像机在PLAY过程中也可以移动来观察天体运动。using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class View : MonoBehaviour {
public float sensitivityMouse = 2f;
public float sensitivetyKeyBoard = 0.1f;
public float sensitivetyMouseWheel = 10f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//按着鼠标右键实现视角转动
if (Input.GetMouseButton(1))
{
transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityMouse, Input.GetAxis("Mouse X") * sensitivityMouse, 0);
}
//键盘按钮←/a和→/d实现视角水平移动,键盘按钮↑/w和↓/s实现视角水平旋转
if (Input.GetAxis("Horizontal") != 0)
{
transform.Translate(Input.GetAxis("Horizontal") * sensitivetyKeyBoard, 0, 0);
}
if (Input.GetAxis("Vertical") != 0)
{
transform.Translate(0, Input.GetAxis("Vertical") * sensitivetyKeyBoard, 0);
}
}
}
【阅读以下游戏脚本】
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!
程序需要满足的要求:
【游戏中提及的对象】
牧师,魔鬼,小船,小河,河岸
【玩家动作表】
动作 | 条件 | 结果 |
---|---|---|
点击岸上的人 | 岸上有人,船上有空位 | 人进入船,岸上人数减少 |
点击船 | 船上有人 | 船移动到对岸 |
点击船上的人 | 船上有人 | 人上岸,船上人数减少 |
public class SSDirector : System.Object {
private static SSDirector Myinstance;
public SceneController FirstController { get; set; }
public static SSDirector getInstance() {
if (Myinstance == null) {
Myinstance = new Director ();
}
return Myinstance;
}
}
public interface SceneController {
void loadResources ();
}
(1)实例化预制对象(以Priest为例)
character = Object.Instantiate(Resources.Load("Priest", typeof(GameObject)), Vector3.zero, Quaternion.identity, null) as GameObject;
(2)角色状态和行为设置
public void setname(string name) //设置对象名称
public void setPosition(Vector3 pos) //设置对象位置
public void moveToPosition(Vector3 dest) //设置对象移动
public int getType() //返回对象角色
public string getName() //返回对象名称
public void getOnBoat() //上船
public void getOnCoast() //上岸
public bool isOnBoat() //判断是否在船上
(1)预制船和设置起始状态
from_positions = new Vector3[] { new Vector3(4.5F, 1.5F, 0), new Vector3(5.5F, 1.5F, 0) };
to_positions = new Vector3[] { new Vector3(-5.5F, 1.5F, 0), new Vector3(-4.5F, 1.5F, 0) };
boat = Object.Instantiate(Resources.Load("Boat", typeof(GameObject)), fromPosition, Quaternion.identity, null) as GameObject;
boat.name = "boat";
(2)设置船的行为
public void Move() //船的移动
public Vector3 getEmptyPosition() //得到船上的孔伟
public bool isEmpty() //判断船是否为空
public void GetOnBoat(CharacterController chCtrl) //调用船的上船状态
public interface UserAction {
void moveBoat();
void characterIsClicked(CharacterController characterCtrl);
void restart();
}
public class ClickGUI : MonoBehaviour {
UserAction action;
CharacterController characterController;
public void setController(CharacterController characterCtrl) {
characterController = characterCtrl;
}
void Start() {
action = Director.getInstance ().FirstController as UserAction;
}
void OnMouseDown() {
if (gameObject.name == "boat") {
action.moveBoat ();
} else {
action.characterIsClicked (characterController);
}
}
}
【游戏展示视频】
见我的Github
https://blog.csdn.net/yaoxh6/article/details/79773632