作业内容
实现物体的抛物线运动。
作业实现
此次作业需要制作出平板和小球,让小球在平板上做抛物线运动,平板和小球的制作较为简单。主要分析运动代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sport : MonoBehaviour
{
public float v0 = 98;//初速度
public float y0 = 0;//初始y坐标
public float x0 = 0;//初始x坐标
public float vx = 0;//x轴的速度
public float vy = 0;//y轴的速度
public float angle = 45;//初速度和x轴的夹角
public float t = 0;//运动的总时长
public float x = 0;//x坐标
public float y = 0;//y坐标
public float temp = 0;//小球运动一个周期时间
// Start is called before the first frame update
void Start()
{
vx = v0 * Mathf.Cos(angle * Mathf.Deg2Rad);
vy = v0 * Mathf.Cos(angle * Mathf.Deg2Rad);
temp = 2 * vy / 9.8f;
}
// Update is called once per frame
void Update()
{
t = t + Time.deltaTime;
x = vx * t;
float T = t % temp;
y = vy * T - 0.5f * 9.8f * Mathf.Pow(T, 2);//使小球不断地往前滚动
Vector3 p = new Vector3(x, y, 0);
if (y >= 0)
{
transform.position = p;
}
}
}
在此次作业中,我让小球可以不断的往前运动,无视小球的物理碰撞,只是简单的向前运动。
场景展示:
视频演示:
抛物线运动
源码:作业3 · BaiLee/3D游戏编程 - 码云 - 开源中国 (gitee.com)
只包含Assets。