前言
中山大学软件工程学院 3D游戏编程与设计课程学习记录博客
游戏代码: 游戏代码
简答题
- 游戏对象运动的本质是什么
- 游戏对象的运动过程本质上就是游戏对象transform属性中的空间位置(Position)、旋转角度(Rotation)、大小(Scale)三个属性随着时间在做某种特定的变化。
- 请用三种方法以上方法,实现物体的抛物线运动
-
使用两个Script修改
this.transform.position
,一个负责物体的自由加速下落,一个负责物体垂直重力方向的匀速运动物体向右匀速运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Moveright : MonoBehaviour
{
private int speed = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.position += Vector3.right * Time.deltaTime * speed;
//this.transform.position += Vector3.up * Time.deltaTime;
}
}
物体自由下落
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fall : MonoBehaviour
{
private float g = 9.8F;
private float speed;
// Start is called before the first frame update
void Start()
{
speed = 0;
}
// Update is called once per frame
void Update()
{
speed += g * Time.deltaTime;
this.transform.position += Vector3.down * Time.deltaTime * speed;
}
}
- 使用一个Script调用
this.transform.Translate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parabola : MonoBehaviour
{
private float g = 9.8F;
private float speed;
private float fallingSpeed;
// Start is called before the first frame update
void Start()
{
speed = 10;
fallingSpeed = 0;
}
// Update is called once per frame
void Update()
{
fallingSpeed += g * Time.deltaTime;
this.transform.Translate(Vector3.left * speed * Time.deltaTime);
this.transform.Translate(Vector3.down * fallingSpeed * Time.deltaTime);
}
}
- 调用
Mathf.MoveTowards
获取下一个位置的坐标并赋值给this.transform.position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fall3 : MonoBehaviour
{
private float g = 9.8F;
private float speed;
private float falling_speed;
// Start is called before the first frame update
void Start()
{
speed = 10;
falling_speed = 0;
}
// Update is called once per frame
void Update()
{
falling_speed += g * Time.deltaTime;
float target_y = Mathf.MoveTowards(this.transform.position.y, this.transform.position.y - falling_speed * Time.deltaTime, speed * Time.deltaTime);
float target_x = Mathf.MoveTowards(this.transform.position.x, this.transform.position.x + speed * Time.deltaTime, falling_speed * Time.deltaTime);
this.transform.position = new Vector3(target_x, target_y, this.transform.position.z);
}
}
- 写一个程序,实现一个完整的的太阳系,其他星球围绕太阳的转速必须不一样,且不在一个法平面
- 脚本代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SolarSystem : MonoBehaviour
{
public Transform Sun;
public Transform Mercury;
public Transform Venus;
public Transform Earth;
public Transform Moon;
public Transform Mars;
public Transform Jupiter;
public Transform Saturn;
public Transform Uranus;
public Transform Neptune;
// Start is called before the first frame update
void Start()
{
init_planetScale();
init_planetPosition();
}
// Update is called once per frame
void Update()
{
Mercury.RotateAround(Sun.position, new Vector3(0.3f, 1, 0), 10 * 365 / 87.7f * Time.deltaTime);
Venus.RotateAround(Sun.position, new Vector3(0.2f, 1, 0), 10 * 365 / 224.7f * Time.deltaTime);
Earth.RotateAround(Sun.position, Vector3.up, 10 * Time.deltaTime);
Earth.Rotate(Vector3.up * 30 * Time.deltaTime);
Moon.RotateAround(Earth.position, Vector3.up, 365 * Time.deltaTime);
Mars.RotateAround(Sun.position, new Vector3(0.5f, 1, 0), 10 * 365 / 686.98f * Time.deltaTime);
Jupiter.RotateAround(Sun.position, new Vector3(0.5f, 1, 0), 10 * 1 / 11.8f * Time.deltaTime);
Saturn.RotateAround(Sun.position, new Vector3(0.6f, 1, 0), 10 * 1 / 29.5f * Time.deltaTime);
Uranus.RotateAround(Sun.position, new Vector3(0.23f, 1, 0), 10 * 1 / 80.4f * Time.deltaTime);
Neptune.RotateAround(Sun.position, new Vector3(0.17f, 1, 0), 10 * 1 / 164.8f * Time.deltaTime);
}
void init_planetScale()
{
this.Sun.localScale = new Vector3(20, 20, 20);
this.Mercury.localScale = new Vector3(1.5F, 1.5F, 1.5F);
this.Venus.localScale = new Vector3(3.6F, 3.6F, 3.6F);
this.Earth.localScale = new Vector3(3.6F, 3.6F, 3.6F);
this.Moon.localScale = new Vector3(0.9F, 0.9F, 0.9F);
this.Mars.localScale = new Vector3(2.1F, 2.1F, 2.1F);
this.Jupiter.localScale = new Vector3(14, 14, 14);
this.Saturn.localScale = new Vector3(12, 12, 12);
this.Uranus.localScale = new Vector3(5, 5, 5);
this.Neptune.localScale = new Vector3(5, 5, 5);
}
void init_planetPosition()
{
this.Sun.position = Vector3.zero;
this.Mercury.position = new Vector3(24, 0.2f, 0.02f);
this.Venus.position = new Vector3(32, 0.03f, 0.3f);
this.Earth.position = new Vector3(40, 0.07f, 0.7f);
this.Moon.position = new Vector3(45, 0.07f, 0.7f);
this.Mars.position = new Vector3(60, 0.09f, 0.9f);
this.Jupiter.position = new Vector3(80, 0.8f, 0.08f);
this.Saturn.position = new Vector3(100, 0.06f, 0.6f);
this.Uranus.position = new Vector3(120, 0.3f, 0.39f);
this.Neptune.position = new Vector3(140, 0.7f, 0.79f);
}
}
- 在菜单栏选择
GameObject
,选择Create Empty
, 将脚本搭载到新建的游戏对象中,再使用GameObject
的3D Object
中的Sphere
新建多个球体来代表行星,并在Unity的Asset Store下载了一个 行星的资源包,并将行星搭载到脚本上 - 运行结果
编程实践
原文地址:http://www.cnblogs.com/joshf/p/16817539.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性