让玩家自由旋转GameObject [英] Let the players freely rotate a GameObject

查看:81
本文介绍了让玩家自由旋转GameObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法,使玩家可以使用鼠标自由拖动和旋转对象(单击以查看场景). 如您所见,移动对象的脚本工作正常,当您要旋转对象时会出现问题.实际上,它可以尝试您尝试旋转对象的时间.如果释放鼠标并重试,则对象会恢复到其初始旋转,然后再次被鼠标旋转.我不要我希望它从原处继续旋转.到目前为止,这是我的代码:

I am trying to find a way so that the player can drag and rotate an object freely using the mouse (click to view scene). As you can see the script to move the object works just fine, the problems come when you want to rotate it. In fact it works the firts time that you try to rotate the object. If you release the mouse and try again, the object snaps back to its initial rotation before being rotated by the mouse again. I don't want that. I want it to continue the rotation from where it is. Here's my code so far:

private void OnMouseDown()
    {
        worldPositionA = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

void Update()
    {
        worldPositionB = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

private void OnMouseDrag()
    {
        Vector3 relativePositionA = worldPositionA - parent.gameObject.transform.position;
        Vector3 relativePositionB = worldPositionB - parent.gameObject.transform.position;

        float angleBetween = Vector2.SignedAngle(relativePositionB, relativePositionA);

        angleBetween += parent.transform.rotation.z;

        parent.transform.rotation = Quaternion.AngleAxis(angleBetween, Vector3.back);
    }

worldPositionA是鼠标开始拖动时的世界位置,worldPositionB是更新的鼠标位置. relativePosition(A和B)相同,但相对于对象的中心. 我知道它们是正确的,因为我有指向它们的光线投射,而angleBetween也是正确的,因为我正在用che console打印它. 怎么了?有什么我没想的吗?

worldPositionA is the world position of the mouse when it starts dragging, worldPositionB is the updated mouse position. relativePosition (A and B) are the same but relative to the center of the object. I know they are correct because I have a raycast that points to those, and the angleBetween is correct too because I'm printing it in che console. What is wrong? Is there something I'm not thinking about?

推荐答案

我仍然不知道以前的脚本出了什么问题,但是我找到了另一种方法来实现自己想要的功能. 我没有直接使用Transform,而是添加了Rigidbody并修改了Rotation值. 这是脚本,以防万一有人想要它:

I still don't know what was wrong with my previous script, but I've found another way to accomplish what I wanted. Instead of working with the Transform directly, I added a Rigidbody and modified the Rotation value. Here's the script in case someone wants it:

[SerializeField] private Camera _mainCamera; //Assigned from Inspector

[SerializeField] private GameObject parent = null; //Assigned from Inspector
private Rigidbody2D parentRb2d;

float startRotation;
float angleBetween;

private void Awake()
{
    parentRb2d = parent.GetComponent<Rigidbody2D>();
}

private void Update()
{
    worldPositionB = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
}

private void OnMouseDown()
{
    worldPositionA = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
    startRotation = parentRb2d.rotation;
}

private void OnMouseDrag()
{
    Vector3 relativePositionA = worldPositionA - parent.gameObject.transform.position;
    Vector3 relativePositionB = worldPositionB - parent.gameObject.transform.position;

    angleBetween = Vector2.SignedAngle(relativePositionB, relativePositionA);

    parentRb2d.MoveRotation(startRotation - angleBetween);
}

如果您的对象方向相反,请尝试在最后一行用'+'切换'-'运算符.

If your object in the opposite direction, try switching the '-' operator in the last line with a '+'.

这篇关于让玩家自由旋转GameObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆