【问题标题】:unity3d - how to clamp x axis from gameobject transform forward perspective?unity3d - 如何从游戏对象变换前向角度夹紧x轴?
【发布时间】:2022-01-03 21:45:17
【问题描述】:

我使用光线投射根据鼠标位置移动了对象 ,我想知道如何在任何给定位置夹紧并旋转相对于物体向前变换的物体 x 轴? ,一般没有相对于世界的任何想法? , 提前致谢。 下面的代码显示了我如何根据鼠标位置实现对象移动的基本方法 现在我想夹住它

 Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
    plane = new Plane(Vector3.up, transform.position);
    float enter;
        if(plane.Raycast(ray,out enter))
        {
            hitPoint = ray.GetPoint(enter);
            transform.position = hitPoint;
        }

【问题讨论】:

    标签: c# unity3d clamp


    【解决方案1】:

    如果我对您的理解正确,您想要实现的是仅在 local x 轴上移动对象。

    然后做这样的事情(看看我惊人的绘画技巧)

    您可以使用Vector3.Project 来实现,例如

    var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    var plane = new Plane(Vector3.up, transform.position);
    
    if(plane.Raycast(ray, out var enter))
    {
        var hitPoint = ray.GetPoint(enter);
    
        var actualPositionDelta = hitPoint - transform.position;
        var mappedPositionDelta = Vector3.Project(actualPositionDelta, transform.right);
    
        transform.position += mappedPositionDelta;
    }
    

    【讨论】:

    • 是的,这正是我所需要的,非常感谢! @derHugo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多