简体   繁体   中英

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. 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.
  • Using the IDragHandler interface and changing the board position to the EventData received when a touch is registered.
  • 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.

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. 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. Attached a script to my Image UI gameobject and assigned the necessary transforms in the inspector and voila, it works.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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