Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 统一玩家移动,速度不固定_Unity3d - Fatal编程技术网

Unity3d 统一玩家移动,速度不固定

Unity3d 统一玩家移动,速度不固定,unity3d,Unity3d,我正在制作一个像《斗殴之星》这样的等距3d游戏,我试图得到同样的动作,但我遇到了一些麻烦。我的球员没有固定的速度,但它似乎是增加。如何解决呢?第一个脚本是虚拟操纵杆的脚本,第二个脚本是玩家移动的脚本。 可能是质量问题吗 操纵杆脚本: using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; public class VirtualJoystick : Mo

我正在制作一个像《斗殴之星》这样的等距3d游戏,我试图得到同样的动作,但我遇到了一些麻烦。我的球员没有固定的速度,但它似乎是增加。如何解决呢?第一个脚本是虚拟操纵杆的脚本,第二个脚本是玩家移动的脚本。 可能是质量问题吗

操纵杆脚本:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{

private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;

private void Start()
{
    bgImg = GetComponent<Image>();
    joystickImg = transform.GetChild(0).GetComponent<Image>();
}

public virtual void OnDrag(PointerEventData ped)
{
    Vector2 pos;
    if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
    {
        pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
        pos.y = (pos.y / bgImg.rectTransform.sizeDelta.x);

        inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        // Move joystickImg
        joystickImg.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * bgImg.rectTransform.sizeDelta.x / 3
                , inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));

    }
}

public virtual void OnPointerDown(PointerEventData ped)
{
    OnDrag(ped);
}

public virtual void OnPointerUp(PointerEventData ped)
{
    inputVector = Vector3.zero;
    joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}

public float Horizontal()
{
    if (inputVector.x != 0)
        return inputVector.x;
    else
        return Input.GetAxis("Horizontal");
}

public float Vertical()
{
    if (inputVector.x != 0)
        return inputVector.z;
    else
        return Input.GetAxis("Vertical");
}
}

如果你打算给你的rb增加力量,你必须手动限制你的角色可以达到的最大速度,否则它会无限期地增加或者直到阻力太大。最简单的方法是使用rb.velocity.magnity设置一个限制,只有当角色低于该限制时,才能添加力

编辑,以更改方向:

rb.AddForce(dir * moveSpeed);

if(rigidbody.velocity.magnitude > maxSpeed){
   rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);
   }
这将在施加力后夹紧刚体的最大速度,这样你将改变方向,但不会加速


还要注意,像您这样使用操纵杆方向可能会使角色在对角移动时移动得更快

如果你想给你的rb增加力量,你必须手动限制你的角色可以达到的最大速度,否则它将无限期地增加,或者直到阻力太大。最简单的方法是使用rb.velocity.magnity设置一个限制,只有当角色低于该限制时,才能添加力

编辑,以更改方向:

rb.AddForce(dir * moveSpeed);

if(rigidbody.velocity.magnitude > maxSpeed){
   rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);
   }
这将在施加力后夹紧刚体的最大速度,这样你将改变方向,但不会加速


还要注意,像您这样使用操纵杆方向可能会使角色在对角移动时移动得更快

如何统一处理角色移动:

  • 你的性格受到物理的影响?尽可能使用刚体,并确保不超过终点速度
  • 您的角色不受物理影响,但会影响其他物理对象?使用运动学
    刚体
    并使用移动
  • 你的角色一点物理都没有?通过
    Transform

  • 因此,在您的情况下,我猜解决方案1或2将适用于您。

    如何在统一中处理角色移动:

  • 你的性格受到物理的影响?尽可能使用刚体,并确保不超过终点速度
  • 您的角色不受物理影响,但会影响其他物理对象?使用运动学
    刚体
    并使用移动
  • 你的角色一点物理都没有?通过
    Transform

  • 因此,在你的情况下,我猜解决方案1或2将对你有效。

    我自己将“rb.AddForce(dir*moveSpeed);”改为“rb.position+=dir*(moveSpeed/10);“

    我自己将“rb.AddForce(dir*moveSpeed);”改为“rb.position+=dir*(moveSpeed/10);”它对固定速度有效,但是现在我不能改变方向啊,是的,这不起作用,因为你需要施加力来改变方向。我将编辑我的帖子,它在固定速度下工作,但现在我不能改变方向,是的,这将不起作用,因为你需要施加力来改变方向。我将编辑我的帖子谢谢你的回答,我不知道它是否有效,但我最终解决了:DD谢谢你的回答,我不知道它是否有效,但我最终解决了:DD谢谢这不是一个好的解决方案,会给你带来问题,我建议你选择我向你建议的解决方案之一。你几乎不应该操纵一个非运动学刚体的位置。但是对于运动学刚体,它在与其他物体碰撞时不会停止。没有问题,使用我建议的第一种解决方案。手动移动位置将导致意外行为,很可能与物理剪裁或对对象施加的极端力有关。这不是一个好的解决方案,会给您带来问题,我建议您选择我向您建议的解决方案之一。你几乎不应该操纵一个非运动学刚体的位置。但是对于运动学刚体,它在与其他物体碰撞时不会停止。没有问题,使用我建议的第一种解决方案。手动移动位置将导致意外行为,很可能与物理剪裁或对对象施加的极端力有关。