简体   繁体   English

有没有办法用刚体移动游戏对象

[英]Is there a way to move a gameobject with a rigidbody

Reference the picture for easier understanding of the case. 请参考图片以更轻松地了解案例。

板 .

I'm making a 3D game and want to be able to move the board up and rotate it on the Z axis using a single finger. 我正在制作3D游戏,希望能够用一个手指向上移动该板并在Z轴上旋转它。 If the finger is slid up - the board goes up. 如果手指向上滑动-面板将上升。 If the finger is slid left or right, the board tilts left or right. 如果手指向左或向右滑动,则面板将向左或向右倾斜。

The whole point is so that the board brings the ball that sits on top of it upwards, and when the board is tilted/rotated to the side, the ball starts sliding and falling off to the side. 关键是使棋盘使位于其顶部的球向上运动,并且当棋盘向一侧倾斜/旋转时,球开始向一侧滑落。

The board and the ball have rigidbodies attached. 板和球均附有刚性体。

What I've tried so far: 到目前为止,我已经尝试过:

  • Using a slider that, when the value is changed the board rotates with Quaternion.Euler. 使用滑块,当更改值时,面板将随着Quaternion.Euler旋转。
  • Using the IDragHandler interface and changing the board position to the EventData received when a touch is registered. 使用IDragHandler界面并将板位置更改为注册触摸时收到的EventData。
  • Tried it with buttons that modify the board position vertically by some distance - it happens instantaneously and the ball falls through the board. 尝试使用可在垂直方向改变板位置一定距离的按钮进行尝试-它会立即发生,并且球会从板上掉落。
  • Created a UI image/joystick and used touch/event data (from IDragHandler) to move the board transform whenever the UI joystick is moved. 创建UI图像/游戏杆,并使用触摸/事件数据(来自IDragHandler)从UI游戏杆移动时移动板变换。

I've tried some more variants, unfortunately I can't recall them right now and none of what I have already tried works. 我已经尝试了更多的变体,但不幸的是,我现在不记得它们了,而我已经尝试过的所有方法都没有用。

I figured it out. 我想到了。 I added a image(UI) game object to an empty game object so it can move from 0 on the X and 0 on the Y axis. 我将图像(UI)游戏对象添加到一个空的游戏对象,以便它可以从X上的0和Y轴上的0移动。 Added 3 transforms - 1 tilted to the left, 1 tilted to the right and 1 in the middle of the top of the screen to serve as guides in the game and then disabled them so they don't show up in the actual game. 添加了3个变换-向左倾斜1个,向右倾斜1个和在屏幕顶部中间的1个,以在游戏中用作指导,然后禁用它们,以使它们不会出现在实际游戏中。 Attached a script to my Image UI gameobject and assigned the necessary transforms in the inspector and voila, it works. 将脚本附加到我的Image UI游戏对象上,并在检查器和瞧中分配了必要的变换,它可以正常工作。

using UnityEngine;
using UnityEngine.EventSystems;

public class MoveStick : MonoBehaviour, IDragHandler, IEndDragHandler {

    public Transform leftTarget;
    public Transform rightTarget;
    public float rotateSpeed;

    [Space]

    Vector2 joyStartPosition;
    public Transform stick;
    public Transform topTarget;
    public float speed;

    bool goingUp;

    void Start()
    {
        joyStartPosition = transform.position;
        goingUp = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
        if (transform.localPosition.y > 80f || transform.localPosition.x > 50 || transform.localPosition.x < -50)
        {
            goingUp = true;

            var step = speed * Time.deltaTime;
            stick.position = Vector3.MoveTowards(stick.position, topTarget.position, step);

            Debug.Log("Going  up: " + goingUp);
        }
        else if (transform.localPosition.y < 80f)
        {
            goingUp = false;
            Debug.Log("Going  up: " + goingUp);
        }
        if (transform.localPosition.x > 80 && transform.localPosition.y > 80)
        {
            stick.rotation = Quaternion.RotateTowards(stick.rotation, rightTarget.rotation, rotateSpeed * Time.deltaTime);
        }
        else if (transform.localPosition.x < -80 && transform.localPosition.y > 80)
        {
            stick.rotation = Quaternion.RotateTowards(stick.rotation, leftTarget.rotation, rotateSpeed * Time.deltaTime);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        transform.position = joyStartPosition;
        eventData.position = joyStartPosition;
        Debug.Log("Joystick is not moving");
        goingUp = false;
    }

}

There is still a lot to smooth and improve, however as a base code it works fine. 还有许多事情需要改进和完善,但是作为基本代码,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM