Move 3D Disc With Touch in Unity

Move 3D Disc With Touch in Unity

To move a 3D disc with touch input in Unity using C#, you can follow these steps:

  1. First, make sure that you have set up touch input in your Unity project. You can do this by going to the Edit > Project Settings > Input menu, and enabling the "Mobile Input" checkbox.

  2. Next, create a new C# script and attach it to your disc GameObject. In the script, you will need to use the Unity Touch API to detect touch events on the screen, such as when the user touches the screen and when they release their finger. You can do this by using the Input.touches array, which contains information about all the current touch events.

  3. To move the disc, you will need to use the Transform component of the GameObject. You can access the Transform component by using the transform property of the GameObject. You can then use the position property of the Transform component to set the position of the disc.

  4. To consider other forces in your simulation, such as gravity or friction, you will need to use physics. You can do this by adding a Rigidbody component to your disc GameObject, and using the Unity Physics API to apply forces to the Rigidbody. For example, you can use the AddForce method of the Rigidbody component to apply a force to the disc.

Here is some sample code that demonstrates how you could use touch input to move a disc with physics in Unity using C#:

Example 1:

using UnityEngine;

public class DiscController : MonoBehaviour
{
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        // Check if the screen is being touched
        if (Input.touchCount > 0)
        {
            // Get the first touch event
            Touch touch = Input.GetTouch(0);

            // Check if the touch is a drag event
            if (touch.phase == TouchPhase.Moved)
            {
                // Get the touch position in screen space
                Vector2 touchPosition = touch.position;

                // Convert the touch position to world space
                Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, 10));

                // Apply a force to the disc towards the touch position
                rb.AddForce((worldTouchPosition - transform.position) * 10);
            }
        }
    }
}

Example 2:


using UnityEngine;

public class DiscController : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // Check if the user is currently touching the screen
        if (Input.touchCount > 0)
        {
            // Get the touch event
            Touch touch = Input.GetTouch(0);

            // Get the position of the touch on the screen
            Vector3 touchPosition = touch.position;

            // Convert the touch position from screen space to world space
            Vector3 worldTouchPosition = Camera.main.ScreenToWorldPoint(touchPosition);

            // Set the position of the disc to the touch position
            transform.position = worldTouchPosition;
        }
    }
}