Unity3d-Learning Homework 2 空间与运动

Homework 2

Unity3d-Learning

空间与运动

1、简答题

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

游戏对象运动的本质,是它随着页面刷新而产生的连续的运动,包括游戏对象的位置、旋转、角度、大小等。

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

方法1 修改Transform属性

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

public class Parabola : MonoBehaviour {

  public float initialSpeed = 30f;
  private float xSpeed;
  private float ySpeed;
  private float angle = Mathf.PI / 3;
  public float gravity = 30f;

  // Use this for initialization
  void Start () {
      xSpeed = initialSpeed * Mathf.Cos(angle);
      ySpeed = initialSpeed * Mathf.Sin(angle);
  }

  // Update is called once per frame
  void Update () {
      //根据斜上抛运动公式
      Vector3 vec = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
      this.transform.position += vec;
      ySpeed -= gravity * Time.deltaTime;
  }
}

方法2 使用向量Vector3的方法:Vector3.Lerp

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

public class Parabola : MonoBehaviour {

  public float initialSpeed = 30f;
  private float xSpeed;
  private float ySpeed;
  private float angle = Mathf.PI / 3;
  public float gravity = 30f;

  // Use this for initialization
  void Start () {
      xSpeed = initialSpeed * Mathf.Cos(angle);
      ySpeed = initialSpeed * Mathf.Sin(angle);
  }

  // Update is called once per frame
  void Update () {
      //根据斜上抛运动公式
      Vector3 vec = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
      transform.position = Vector3.Lerp (transform.position, transform.position + vec, 1);
      ySpeed -= gravity * Time.deltaTime;
  }
}

方法3 使用transform.Translate方法

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

public class Parabola : MonoBehaviour {

  public float initialSpeed = 30f;
  private float xSpeed;
  private float ySpeed;
  private float angle = Mathf.PI / 3;
  public float gravity = 30f;

  // Use this for initialization
  void Start () {
      xSpeed = initialSpeed * Mathf.Cos(angle);
      ySpeed = initialSpeed * Mathf.Sin(angle);
  }

  // Update is called once per frame
  void Update () {
      //根据斜上抛运动公式
      Vector3 vec = new Vector3(Time.deltaTime * xSpeed, Time.deltaTime * ySpeed, 0);
      this.transform.Translate(vec);
      ySpeed -= gravity * Time.deltaTime;
  }
}

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

代码

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

public class RoundSun : MonoBehaviour {

  public Transform sun;
  public Transform moon;
  public Transform mercury;//水星
  public Transform venus;//金星
  public Transform earth;//地球
  public Transform mars;//火星
  public Transform jupiter;//木星
  public Transform saturn;//土星
  public Transform uranus;//天王星
  public Transform neptune;//海王星

  // Use this for initialization
  void Start () {
      sun.position = Vector3.zero;
  }

  // Update is called once per frame
  void Update () {
      mercury.RotateAround (sun.position, new Vector3(0, 5, 1), 20 * Time.deltaTime);
      mercury.Rotate ( new Vector3(0, 5, 1) * 5 * Time.deltaTime);

      venus.RotateAround (sun.position, new Vector3(0, 2, 1), 15 * Time.deltaTime);
      venus.Rotate (new Vector3(0, 2, 1) * Time.deltaTime);

      earth.RotateAround (sun.position, Vector3.up, 10 * Time.deltaTime);
      earth.Rotate (Vector3.up * 30 * Time.deltaTime);
      moon.transform.RotateAround (earth.position, Vector3.up, 359 * Time.deltaTime);

      mars.RotateAround (sun.position, new Vector3(0, 12, 5), 9 * Time.deltaTime);
      mars.Rotate (new Vector3(0, 12, 5) * 40 * Time.deltaTime);

      jupiter.RotateAround (sun.position, new Vector3(0, 10, 3), 8 * Time.deltaTime);
      jupiter.Rotate (new Vector3(0, 10, 3) * 30 * Time.deltaTime);

      saturn.RotateAround (sun.position, new Vector3(0, 3, 1), 7 * Time.deltaTime);
      saturn.Rotate (new Vector3(0, 3, 1) * 20 * Time.deltaTime);

      uranus.RotateAround (sun.position, new Vector3(0, 10, 1), 6 * Time.deltaTime);
      uranus.Rotate (new Vector3(0, 10, 1) * 20 * Time.deltaTime);

      neptune.RotateAround (sun.position, new Vector3(0, 8, 1), 5 * Time.deltaTime);
      neptune.Rotate (new Vector3(0, 8, 1) * 30 * Time.deltaTime);
  }
}

运行效果

这里写图片描述
这里写图片描述

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!

程序需要满足的要求:

  • play the game ( http://www.flash-game.net/game/2535/priests-and-devils.html )

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

    Priests

    Devils

    Boat

    River

    Coast

  • 用表格列出玩家动作表(规则表),注意,动作越少越好

    条件动作结果
    右岸有人,船在右岸且未载满右岸上船船上多一人,右岸少了这个人
    船上有人,船在右岸右岸下船右岸多一人,船上少了这个人
    船上有人,船靠岸边开船船和船上的人驶向对岸
    船上有人,船在左岸左岸下船左岸多一人,船上少了这个人
    左岸有人,船在左岸且未载满左岸上船船上多一人,左岸少了这个人
  • 请将游戏中对象做成预制

    这里写图片描述

    • 效果图

    这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值