【发布时间】:2015-03-21 02:48:34
【问题描述】:
我在脚本中实例化的预制件存在问题。我正在尝试做的是模拟炮弹弹丸运动背后的物理原理。我的预制件附有以下脚本:
using UnityEngine;
using System.Collections;
public class ShootPhysics : MonoBehaviour {
// vectors & angles to be calculated
private float fx = 0f;
private float fy = 0f;
private float angle;
// forces acting upon object
private float gravity = 9.8f;
private float force = 1.2f;
// new distance & vector variables
private float dis_x = 0f;
private float dis_y = 0f;
// Script & CannonBall transform
private CannonPhysics cannon_angle;
void Start() {
cannon_angle = GameObject.FindWithTag("Gun").GetComponent<CannonPhysics>();
angle = cannon_angle.getAngle ();
fx = Mathf.Round (force * Mathf.Cos(Mathf.Deg2Rad * angle));
fy = Mathf.Round (force * Mathf.Sin(Mathf.Deg2Rad * angle));
}
void FixedUpdate() {
dis_x = (float)(fx * Time.timeSinceLevelLoad);
dis_y = (float)(fy + Time.timeSinceLevelLoad + (0.5) * (-gravity) * Time.timeSinceLevelLoad * Time.timeSinceLevelLoad);
Debug.Log (dis_x+", "+dis_y);
gameObject.transform.Translate (new Vector2(dis_x, dis_y));
}
}
并且预制件将从以下脚本中实例化:
using UnityEngine;
using System.Collections;
public class CannonPhysics : MonoBehaviour {
// This will be our script to rotate our cannon
private float angle = 0f;
// cannonball object
public Transform FirePoint;
GameObject cannon = null;
void Update() {
if(Input.GetKey(KeyCode.UpArrow)) {
angle = angle + 1;
}
if(Input.GetKey(KeyCode.DownArrow)) {
angle = angle - 1;
}
// update cannon angle
transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle));
if(Input.GetMouseButtonDown(0)) {
CreateCannon();
}
}
public float getAngle() {
return angle;
}
private void CreateCannon() {
cannon = Instantiate (Resources.Load("Prefabs/CannonBall"), FirePoint.position, FirePoint.rotation) as GameObject;
Destroy (cannon, 3f);
}
}
对象被实例化,并且对象在 3 秒后被销毁,但是对象 x 和 y 的位置根本没有改变,也不是我所期望的.举个例子吧:
这些是x 和y 坐标,我应该期望它们在控制台中显示。
但是这些是 Unity 检查器中显示的实际坐标:
同样奇怪的是,这些坐标不会因每个对象而改变,它们实际上会保持不变,即使在对象被破坏之后也是如此。谁能解释为什么会这样?或许可以提供解决方案?
编辑:更清晰的例子:
每次我实例化我的炮弹对象时,下一个要实例化的对象的x 和y 坐标都是从最后一个对象引用的。这是我的第一个实例化对象被销毁后的结果。
在实例化第二个对象之后。
现在我期望实例化的每个新对象都应从其“原始”位置(形象地说)开始,x 和 y 值应重置并有效地重新开始。
【问题讨论】:
-
使用
Translate,您将根据给定的数量更改x和y,而不是将它们设置为x和y。与实际的x和y位置相比,它们的数量相对较少,所以您可能没有注意到游戏窗口中的差异? -
谢谢,但是为什么在每个实例化的新对象上引用相同的
x和y位置?就像我实例化一个对象一样,下一个对象将引用被销毁后前一个对象留下的x和y线。 -
理想情况下,我想要的是每个实例化的新对象都从引用的“原始”线开始
-
它们都在同一个地方被实例化的原因很简单——你告诉
Instantiate在FirePoint.position创建它们,它永远不会改变。 -
嗯,是的,这就是我想要的,问题是每个
x和y绳子都是从最后一个实例化的对象中引用的。我不认为我解释得很好。我将尝试用更清晰的示例更新 OP。
标签: c# unity3d game-physics