问题描述
限时送ChatGPT账号..我目前正在从事一个学校计算机科学项目,我决定为此制作一个简单的弹丸模拟器,用户只需提供初始发射速度、发射角度和重力.到目前为止,我已经尝试制作一个脚本,使用这些值来计算轨迹模式并随时间改变球体在平面上的 y 和 z 位置.
Hi I'm currently working on a school computer science project for which I have decided to make a simple projectile simulator, where the user just provides the initial launch speed, the launch angle, and gravity. So far I've tried to make a script that uses these values to calculate a trajectory pattern and change the y and z position of a sphere across a plane over time.
public class SphereJump : MonoBehaviour{
public float gravity = 9.8f;
public float InitialSpeed = 10.0f;
public float LaunchAngle = 45.0f;
public Transform Sphere;
void Start () {
float InitialX = Mathf.Sin(LaunchAngle) * InitialSpeed;
float InitialY = Mathf.Cos(LaunchAngle) * InitialSpeed;
float Range = Mathf.Sin(2*LaunchAngle)*Mathf.Pow(InitialSpeed,2)/gravity;
float MaxHeight = Mathf.Sin(LaunchAngle) * Mathf.Pow(InitialSpeed, 2) / 2 * gravity;
float FlightTime = Range / InitialX;
float ElapsedTime = 0;
while (ElapsedTime < FlightTime)
{
float NewPositionX = transform.position.z+InitialX*ElapsedTime;
float NewPositionY = transform.position.y +InitialY -gravity / 2*ElapsedTime;
Sphere.Translate(0f, NewPositionY, NewPositionX);
ElapsedTime += Time.deltaTime;
}
}
}
对 Unity 来说是全新的,在我看来,这在数学上应该是可行的,但是当我运行游戏时,要么什么也没发生,要么球体对象消失.一开始,它的位置是 (1, 0.5, 1) - 仅比它所在的平面高 0.5 - 据我所知,脚本已正确附加.还会出现一个警告说由于浮点精度限制......".使用 Vector3 会有帮助吗?我是否完全混淆了脚本?
Being completely new to Unity, in my head this should mathematically work, however when I run the game either nothing happens or the sphere object disappears. At the start it's position is (1, 0.5, 1) - which is just 0.5 above the plane it's sitting on - and as far as I'm aware the script is correctly attached. A warning also comes up saying "Due to floating point precision limitations...". Would using Vector3 help? Have I confused the script entirely?
推荐答案
这应该放在 Update()
中,您可以自己添加该方法并移动代码.
this should go in an Update()
you can add that method yourself and move the code.
至于你的第二个问题:
矢量 3 是 3d 空间中一个点位置的坐标系,X、Y 和 Z!您将需要一个 z 坐标来表示深度,否则 unity 不知道将对象放在哪里
Vector 3 is a coordinate system for a point position in 3d space, X,Y and Z! you will need a z coordinate to denote depth, or unity dosnt know where to place your object
所以:
Vector3 newPos= new Vector3(x,y,z);
如果您不希望与相机的距离发生变化,z 可以保持不变.
z can remain constant if you dont want distance from the camera to vary.
Vector3 newPos= new Vector3(transform.position.z+InitialX*ElapsedTime,transform.position.y +InitialY -gravity / 2*ElapsedTime,z);
然而,
在 unity 中有一个叫做刚体的组件.将此附加到您的对象将增加重力和物理.
in unity there is a component called rigidbody. attaching this to your object will add gravity and physics.
然后您可以在脚本中获取刚体:
you can then get the rigidbody in script:
RigidBody body = this.GameObject.GetComponent
那么你可以这样做:
body.AddForce(position,direction,force);
或 body.gravity=3;
或其他一些调整以融入您的物理挑战.
or several other adjustments to incorperate your physics challenge.
这篇关于需要帮助使球体在抛物线中移动作为射弹模拟器的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论