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

Unity(C#):基于旧坐标与距离获取对齐原点的新玩家坐标

Adjust Player's Circular Orbit Radius in Unity (C#)

Got it, let's break this down clearly. You want to keep the player's direction relative to the origin exactly the same, just change their distance from the origin to match your target radius. This is straightforward with basic vector operations in Unity—here's how to do it step by step:

Core Concept

The key idea is to:

  1. Calculate the direction vector from the origin to the player's current position.
  2. Normalize that vector (turn it into a unit vector with length 1) to isolate the pure direction.
  3. Scale this unit vector by your target radius to get the desired offset from the origin.
  4. Add that scaled vector to the origin's position to get the player's new coordinates.

C# Implementation (3D)

Here's a ready-to-use script snippet that handles this, including edge cases:

using UnityEngine;

public class OrbitRadiusAdjuster : MonoBehaviour
{
    [SerializeField] private Transform originTransform; // Assign your origin object in the Inspector
    [SerializeField] private Transform playerTransform; // Assign your player object in the Inspector
    [SerializeField] private float targetRadius = 6f; // Your desired distance from origin
    [SerializeField] private float smoothAdjustSpeed = 2f; // Optional: for smooth transitions

    void Update()
    {
        // Uncomment to adjust radius instantly
        // UpdatePlayerPositionInstantly();
        
        // Use this for smooth, gradual radius adjustment
        UpdatePlayerPositionSmoothly();
    }

    private void UpdatePlayerPositionInstantly()
    {
        // Get the vector pointing from origin to player
        Vector3 originToPlayer = playerTransform.position - originTransform.position;

        // Handle edge case: player is exactly at origin (avoid division by zero)
        if (originToPlayer.magnitude < Mathf.Epsilon)
        {
            // Fallback to a default direction (e.g., positive X-axis)
            originToPlayer = Vector3.right;
        }
        else
        {
            // Normalize to get a direction-only vector (length = 1)
            originToPlayer.Normalize();
        }

        // Calculate new position: origin + (direction * target radius)
        Vector3 newPlayerPos = originTransform.position + originToPlayer * targetRadius;

        // Apply the new position
        playerTransform.position = newPlayerPos;
    }

    private void UpdatePlayerPositionSmoothly()
    {
        Vector3 originToPlayer = playerTransform.position - originTransform.position;

        if (originToPlayer.magnitude < Mathf.Epsilon)
        {
            originToPlayer = Vector3.right;
        }
        else
        {
            originToPlayer.Normalize();
        }

        Vector3 targetPosition = originTransform.position + originToPlayer * targetRadius;

        // Smoothly interpolate between current position and target
        playerTransform.position = Vector3.Lerp(
            playerTransform.position, 
            targetPosition, 
            smoothAdjustSpeed * Time.deltaTime
        );
    }
}

2D Game Adaptation

If you're working on a 2D game, the logic is identical—just swap Vector3 with Vector2 where appropriate:

private void UpdatePlayerPosition2D()
{
    Vector2 originToPlayer = (Vector2)playerTransform.position - (Vector2)originTransform.position;

    if (originToPlayer.magnitude < Mathf.Epsilon)
    {
        originToPlayer = Vector2.right;
    }
    else
    {
        originToPlayer.Normalize();
    }

    Vector2 newPlayerPos = (Vector2)originTransform.position + originToPlayer * targetRadius;
    playerTransform.position = newPlayerPos;
}

Key Notes

  • Normalization: This step ensures we only keep the direction from the origin to the player, stripping out the current distance information. Multiplying by your target radius gives us exactly the offset we need.
  • Edge Case Handling: When the player is at the origin, the direction vector has a magnitude of 0, which would break normalization. We fix this by falling back to a default direction (you can change this to match your game's needs, like Vector3.forward for 3D).
  • Smooth Adjustment: Using Vector3.Lerp with Time.deltaTime ensures the player moves smoothly to the new radius instead of teleporting, which feels better for most games.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 08:32:28