【发布时间】:2015-12-05 22:37:00
【问题描述】:
我希望在每一帧中移动、缩放和旋转给定的圆柱体,使其表现得像两点之间的“绳索”。
我现在有这段代码,但它根本不像预期的那样工作:
hook.transform.position = (rightHandPosition + hookDestination)/2;
hook.transform.localScale = new Vector3(0.5F, Vector3.Magnitude(hookDestination - rightHandPosition), 0.5F);
hook.transform.rotation = Quaternion.Euler(hookDestination - rightHandPosition);
你可以猜到这两个点是 rightHandPosition 和 hookDestination。目前,圆柱体在“随机”位置生成,具有“随机”旋转和巨大的比例。
我该如何解决?
“完整”脚本:
public class FirstPersonController : MonoBehaviour {
public GameObject hook;
bool isHooked = false;
Vector3 hookDestination;
Vector3 rightHandPosition;
void Start() {
hook.renderer.enabled = false;
rightHandPosition = hook.transform.position;
}
// Update is called once per frame
void Update () {
if (isHooked) {
hook.transform.position = (rightHandPosition + hookDestination)/2;
hook.transform.localScale = new Vector3(0.5F, Vector3.Magnitude(hookDestination - rightHandPosition), 0.5F);
hook.transform.rotation = Quaternion.Euler(hookDestination - rightHandPosition);
}
if (isHooked && !Input.GetMouseButton(1)) {
isHooked = false;
hook.renderer.enabled = false;
}
if (Input.GetMouseButtonDown (1) && !isHooked) {
Ray ray = GameObject.FindGameObjectWithTag ("MainCamera").camera.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast (ray, out hit) && hit.distance < 5000000 && hit.collider.tag != "Player") {
isHooked = true;
hookDestination = hit.point;
hook.renderer.enabled = true;
}
}
}
}
场景截图:
【问题讨论】:
-
完整的脚本和游戏视图的图像会有所帮助
-
我没有对此进行测试,但请尝试:
Quaternion.SetLookRotation(hookDestination - rightHandPosition)或者transform.localRotation而不是rotation。我支持@BurakKarasoy 的评论。 -
@TheOddler :不起作用(似乎根本没有应用旋转)。我用脚本的大部分内容和屏幕截图更新了我的原始帖子:)
-
@fafase:现在这是一个有趣的选择!我会尝试一下,让你知道它是怎么回事。但是,我仍然想在我的原始脚本中找到错误,只是为了我个人的知识:)
标签: c# unity3d gameobject