将光标限制在我的播放器周围的半径范围内

Sam*_*son 5 c# unity-game-engine

我目前正在开发一款多人射击游戏。基本上 atm 你扮演一个立方体,你的手(红色方块)跟随光标进入游戏。

我想将光标移动限制为围绕我的播放器精灵的完美圆圈。

请参阅附图以进行说明。

https://imgur.com/TLliade

以下脚本附在“手”(红色方块)上。

public class Handscript : MonoBehaviour
{

    void Update()
    {
        Cursor.visible = false;

        Vector3 a = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
        a.Set(a.x, a.y, transform.position.z);
        transform.position = Vector3.Lerp(transform.position, a, 1f);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想限制光标在我的半径内移动,这是一个附加到我的 Player 预制件的公共变换,如下所示:

    //Hand radius
    public float radius;
    public Transform centerRadius;
Run Code Online (Sandbox Code Playgroud)

我很难被困住并且对整体编码不熟悉,我可以朝正确的方向推动。

如果我不清楚,这里会问基本相同的问题:https : //answers.unity.com/questions/1439356/limit-mouse-movement-around-player.html

编辑:我的最终目标是拥有与传说中的“疯狂互动”游戏类似的手部动作,可在此处找到:https : //www.newgrounds.com/portal/view/118826

EDIT2:可能无法将光标锁定在半径圆内。是否可以将游戏对象“手”锁定在此半径内?

EDIT3:这是我使用的代码,它的作用就像一个魅力:

using System;
using UnityEngine;

public class Handscript : Photon.MonoBehaviour
{
    [SerializeField] private GameObject Player2;        //Drag your player game object here for its position
    [SerializeField] private float radius;              //Set radius here

    public new PhotonView photonView;                   //Never mind this if its not a photon project

    private void Start()
    {
        Cursor.visible = false;
    }

    void Update()
    {
            if (photonView.isMine)      //Never mind this if statement if it isnt a photon project
            {
                Vector3 cursorPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                Vector3 playerPos = Player2.transform.position;

                Vector3 playerToCursor = cursorPos - playerPos;
                Vector3 dir = playerToCursor.normalized;
                Vector3 cursorVector = dir * radius;

                if (playerToCursor.magnitude < cursorVector.magnitude) // detect if mouse is in inner radius
                    cursorVector = playerToCursor;

                transform.position = playerPos + cursorVector;

            }
    }




#if UNITY_EDITOR
    private void OnDrawGizmosSelected()
    {
        UnityEditor.Handles.DrawWireDisc(transform.parent.position, Vector3.back, radius); // draw radius
    }
#endif
}

Run Code Online (Sandbox Code Playgroud)

Igg*_*ggy 5

  1. 获取从玩家到光标的向量:

    Vector3 playerToCursor = cursorPos - playerPos;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将其标准化以获得方向:

    Vector3 dir = playerToCursor.normalized;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将方向乘以您想要的半径:

    Vector3 cursorVector = dir * radius;
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将光标向量添加到玩家位置以获得最终位置:

    Vector3 finalPos = playerPos + cursorVector;
    
    Run Code Online (Sandbox Code Playgroud)

图片

  • 感谢您的回答,我正在尝试将此代码放入我的 PlayerController 脚本或我的“Handmovement”脚本中。 (2认同)
  • 哇,这是一张很棒的照片,谢谢!我在分配变量方面仍然很困难。如前所述,我非常缺乏经验 (2认同)