gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:37:37 27 4
gpt4 key购买 nike

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

我想将光标移动限制在我的玩家 Sprite 周围的一个完美圆圈内。

详见附图。

https://imgur.com/TLliade

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

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);
}
}

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

    //Hand radius
public float radius;
public Transform centerRadius;

我很难坚持,而且对整体编码还很陌生,我可以朝着正确的方向努力。

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

编辑:我最终的目标是让手部 Action 与传说中的“Madness Interactive”游戏相似,可在此处找到: 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
}

最佳答案

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

    Vector3 playerToCursor = cursorPos - playerPos;
  2. 对其进行归一化以获得方向:

    Vector3 dir = playerToCursor.normalized;
  3. 将方向乘以所需的半径:

    Vector3 cursorVector = dir * radius;
  4. 将游标向量添加到玩家位置得到最终位置:

    Vector3 finalPos = playerPos + cursorVector;

pic

关于c# - 将光标限制在我的播放器周围的半径范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57593968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com