【问题标题】:Unity3D : make cylinder stretch from one point to anotherUnity3D:使圆柱体从一点拉伸到另一点
【发布时间】: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


【解决方案1】:

假设cubeStart 是起点,cubeEnd 是终点。而“圆柱体”就是你的圆柱体,那么

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AlignCyl : MonoBehaviour {


    GameObject cubeStart;
    GameObject cubeEnd;
    GameObject cylinder;

    Vector3 endV; 
    Vector3 startV;
    Vector3 rotAxisV;
    Vector3 dirV;
    Vector3 cylDefaultOrientation = new Vector3(0,1,0);

    float dist;

    // Use this for initialization
    void Start () {

         cubeStart = GameObject.Find("CubeStart");
         cubeEnd   = GameObject.Find("CubeEnd");

         cylinder  = GameObject.Find("Cylinder");

        endV   = cubeEnd.transform.position;
        startV = cubeStart.transform.position;


    }

    // Update is called once per frame
    void Update () {

        // Position
        cylinder.transform.position = (endV + startV)/2.0F;

        // Rotation
        dirV = Vector3.Normalize(endV - startV);

        rotAxisV = dirV + cylDefaultOrientation;

        rotAxisV = Vector3.Normalize(rotAxisV);

        cylinder.transform.rotation = new Quaternion(rotAxisV.x, rotAxisV.y, rotAxisV.z, 0);

        // Scale        
        dist = Vector3.Distance(endV, startV);

        cylinder.transform.localScale = new Vector3(1, dist/2, 1);

    }
}

使用 Unity3D 2018.1 测试 我关于旋转的概念完全基于四元数。

x=rotAxis.x * sin(angle/2)  = rotAxis.x;
y=rotAxis.y * sin(angle/2) = rotAxis.y;
z=rotAxis.z * sin(angle/2) = rotAxis.z;
w = cos(angle/2) = 0;

其中 rotAxis 是 2 个向量的总和,即默认圆柱方向和所需方向。角度是 180 度,因为我们希望圆柱体围绕 rotAxis 旋转 180 度。它是四元数旋转(绕轴旋转)的定义。

【讨论】:

    【解决方案2】:

    fafase 的评论是正确的答案:使用LineRenderer

    hookRender.SetPosition(0, rightHandPosition);
    hookRender.SetPosition(1, hookDestination);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2011-02-10
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多