You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D C#技术实现:如何在固定点控制平面进行上下左右移动?

How to Control a Plane's Movement (Up/Down, Left/Right) Around a Fixed Point in Unity3D with C#

Hey there! I get it—you're building Rolling Ball 3D Maze Puzzle and stuck on how to move a plane (your maze, I assume) up/down and left/right while keeping a fixed point locked in place. Let's break this down into easy-to-implement steps using Unity C#.

Approach 1: Translate the Plane While Keeping a Fixed Point Locked

This method lets you move the plane (translate it) such that a specific world point stays in the same position—ideal if you want the maze to shift around your rolling ball, keeping the ball centered relative to the camera or world.

Step-by-Step Implementation

  1. Attach this script to your plane GameObject.
  2. Assign your fixed anchor point (e.g., your ball's Transform) in the Inspector.
  3. Adjust the moveSpeed and axis settings to match your game's needs.
using UnityEngine;

public class PlaneAnchorMovement : MonoBehaviour
{
    // Assign the transform you want to keep fixed (e.g., the rolling ball)
    public Transform fixedAnchorPoint;
    // Control how fast the plane moves
    public float moveSpeed = 4f;

    private Vector3 _anchorLockedWorldPos;

    void Start()
    {
        // Store the initial world position of our fixed anchor point
        if (fixedAnchorPoint != null)
        {
            _anchorLockedWorldPos = fixedAnchorPoint.position;
        }
        else
        {
            // Fallback to a static world point if no anchor is set
            _anchorLockedWorldPos = new Vector3(0, 0, 0);
            Debug.LogWarning("No fixed anchor point assigned—using origin as default.");
        }
    }

    void Update()
    {
        // Get player input for horizontal (left/right) and vertical (up/down) movement
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        // Define movement direction (adjust axes based on your plane's orientation)
        // For X-Z plane movement (left/right = X, forward/back = Z), use this:
        Vector3 moveDirection = new Vector3(horizontal, 0, vertical).normalized;
        // For Y-axis up/down movement, swap Z with Y:
        // Vector3 moveDirection = new Vector3(horizontal, vertical, 0).normalized;

        if (moveDirection.magnitude > 0.1f)
        {
            // Calculate the raw movement we want to apply to the plane
            Vector3 rawMovement = moveDirection * moveSpeed * Time.deltaTime;

            // Get the anchor point's position relative to the plane (local space)
            Vector3 anchorLocalPos = transform.InverseTransformPoint(fixedAnchorPoint.position);

            // Apply the raw movement to the plane
            transform.Translate(rawMovement, Space.World);

            // Calculate where the anchor would be after this movement
            Vector3 newAnchorWorldPos = transform.TransformPoint(anchorLocalPos);

            // Correct the plane's position to lock the anchor back to its original world position
            Vector3 correction = _anchorLockedWorldPos - newAnchorWorldPos;
            transform.Translate(correction, Space.World);
        }
    }
}

Key Notes

  • Axis Adjustment: Swap axes in the moveDirection vector if your plane uses a different orientation (e.g., Y-axis for up/down instead of Z).
  • Static Fixed Point: If you don't have a Transform for the anchor, replace fixedAnchorPoint.position with a static Vector3 (like new Vector3(2, 0, 5)).

Bonus: Tilting the Plane Around a Fixed Point (Common Maze Mechanic)

Wait—if you're making a rolling ball maze game, tilting the plane (rotating it to let gravity move the ball) is far more typical than translating the plane. If that's what you actually need, here's a quick implementation:

using UnityEngine;

public class MazeTiltController : MonoBehaviour
{
    public Transform fixedAnchorPoint; // Point to rotate around (e.g., ball's position)
    public float tiltSpeed = 3f;
    public float maxTiltAngle = 12f; // Prevent overly steep tilts

    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // Calculate target tilt angles
        float targetTiltX = verticalInput * maxTiltAngle;
        float targetTiltY = horizontalInput * maxTiltAngle;

        // Smoothly rotate the plane around the fixed anchor point
        transform.RotateAround(
            fixedAnchorPoint.position, 
            Vector3.right, 
            (targetTiltX - transform.eulerAngles.x) * tiltSpeed * Time.deltaTime
        );
        transform.RotateAround(
            fixedAnchorPoint.position, 
            Vector3.up, 
            (targetTiltY - transform.eulerAngles.y) * tiltSpeed * Time.deltaTime
        );
    }
}

Why This Works

  • RotateAround() is Unity's built-in method for rotating an object around a specific world point—perfect for maze tilting.
  • maxTiltAngle ensures you don't tilt the maze so much the ball flies off-screen or gets stuck.

Whichever method you choose, test it with your ball and adjust the speed/angle values until the movement feels responsive and fun. Small tweaks can make a huge difference in gameplay feel!

内容的提问来源于stack exchange,提问作者Adam Wallwork

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:57:46