球跳跃时移动得太远。它在空气中时仍然可以控制。Unity3d播放器控制脚本

球跳跃时移动得太远。它在空气中时仍然可以控制。Unity3d播放器控制脚本,unity3d,unityscript,Unity3d,Unityscript,我在搞统一球赛。我的球员是一个球,它使用球员控制脚本。当球在空中跳跃时,它仍然可以被控制,并且在空中移动到任何方向。我不希望这样,因为它无法达到投掷迷宫的目的,因为它可以飞越障碍物 我正在使用一个免费的unity游戏套件附带的玩家控制脚本。我试图修复它,但我只能删除跳转功能或降低其高度,无法修复该问题 using UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { pr

我在搞统一球赛。我的球员是一个球,它使用球员控制脚本。当球在空中跳跃时,它仍然可以被控制,并且在空中移动到任何方向。我不希望这样,因为它无法达到投掷迷宫的目的,因为它可以飞越障碍物

我正在使用一个免费的unity游戏套件附带的玩家控制脚本。我试图修复它,但我只能删除跳转功能或降低其高度,无法修复该问题

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour 
{
    private GameObject moveJoy;
    private GameObject _GameManager;
    public Vector3 movement;
    public float moveSpeed = 6.0f;
    public float jumpSpeed = 5.0f;
    public float drag = 2;
    private bool canJump = true;

    void Start()
    {
        moveJoy = GameObject.Find("LeftJoystick");
        _GameManager = GameObject.Find("_GameManager");
    }

    void Update () 
    {   
        Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
        forward.y = 0;
        forward = forward.normalized;

        Vector3 forwardForce = new Vector3();
        if (Application.platform == RuntimePlatform.Android) 
        {
            float tmpSpeed = moveJoy.GetComponent<Joystick>().position.y;
            forwardForce = forward * tmpSpeed * 1f * moveSpeed;
        }
        else
        {
            forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
        }
        rigidbody.AddForce(forwardForce);

        Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
        right.y = 0;
        right = right.normalized;

        Vector3 rightForce = new Vector3();
        if (Application.platform == RuntimePlatform.Android) 
        {
            float tmpSpeed = moveJoy.GetComponent<Joystick>().position.x;
            rightForce = right * tmpSpeed * 0.8f * moveSpeed;
        }
        else
        {
            rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
        }       
        rigidbody.AddForce(rightForce);

        if (canJump && Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.AddForce(Vector3.up * jumpSpeed * 100);
            canJump = false;
            _GameManager.GetComponent<GameManager>().BallJump();
        }
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.tag == "Destroy")
        {
            _GameManager.GetComponent<GameManager>().Death();
            Destroy(gameObject);
        }
        else if (other.tag == "Coin")
        {
            Destroy(other.gameObject);
            _GameManager.GetComponent<GameManager>().FoundCoin();
        }
        else if (other.tag == "SpeedBooster")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().SpeedBooster();
        }
        else if (other.tag == "JumpBooster")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().JumpBooster();
        }
        else if (other.tag == "Teleporter")
        {
            movement = new Vector3(0,0,0);
            _GameManager.GetComponent<GameManager>().Teleporter();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (!canJump)
        {
            canJump = true;
            _GameManager.GetComponent<GameManager>().BallHitGround();
        }
    }

    void OnGUI()
    {
        GUI.Label(new Rect(300,10,100,100),"X: " + moveJoy.GetComponent<Joystick>().position.x.ToString());
        GUI.Label(new Rect(300,30,100,100),"Y: " + moveJoy.GetComponent<Joystick>().position.y.ToString());
    }
}
使用UnityEngine;
使用系统集合;
公共类玩家控制:单一行为
{
私人游戏对象;
私人游戏对象(GameManager),;
公共矢量运动;
公共浮动速度=6.0f;
公共浮子跳跃速度=5.0f;
公众浮标阻力=2;
私有bool canJump=true;
void Start()
{
moveJoy=GameObject.Find(“左操纵杆”);
_GameManager=GameObject.Find(“\u GameManager”);
}
无效更新()
{   
Vector3 forward=Camera.main.transform.TransformDirection(Vector3.forward);
正向y=0;
向前=向前。标准化;
Vector3 forwardForce=新Vector3();
if(Application.platform==RuntimePlatform.Android)
{
float tmpSpeed=moveJoy.GetComponent().position.y;
前进力=前进*tmpSpeed*1f*移动速度;
}
其他的
{
forwardForce=前进*输入。GetAxis(“垂直”)*移动速度;
}
刚体附加力(前向力);
Vector3 right=Camera.main.transform.TransformDirection(Vector3.right);
对,y=0;
right=right.normalized;
Vector3 rightForce=新Vector3();
if(Application.platform==RuntimePlatform.Android)
{
float tmpSpeed=moveJoy.GetComponent().position.x;
右力=右*tmpSpeed*0.8f*移动速度;
}
其他的
{
rightForce=right*输入。GetAxis(“水平”)*移动速度;
}       
刚体。附加力(rightForce);
if(canJump&&Input.GetKeyDown(KeyCode.Space))
{
刚体.AddForce(矢量3.up*跳跃速度*100);
canJump=false;
_GameManager.GetComponent().BallJump();
}
}
无效对撞机(对撞机其他)
{
如果(other.tag==“销毁”)
{
_GameManager.GetComponent().Death();
摧毁(游戏对象);
}
else if(other.tag==“Coin”)
{
销毁(其他游戏对象);
_GameManager.GetComponent().FoundCoin();
}
else if(other.tag==“SpeedBooster”)
{
运动=新矢量3(0,0,0);
_GameManager.GetComponent().SpeedBooster();
}
else if(other.tag==“JumpBooster”)
{
运动=新矢量3(0,0,0);
_GameManager.GetComponent().JumpBooster();
}
else if(other.tag==“传送者”)
{
运动=新矢量3(0,0,0);
_GameManager.GetComponent().Teleporter();
}
}
无效碰撞中心(碰撞)
{
如果(!canJump)
{
canJump=true;
_GameManager.GetComponent().BallHitGround();
}
}
void OnGUI()
{
标签(新的Rect(300,10100100),“X:+moveJoy.GetComponent().position.X.ToString());
标签(新的Rect(300,30100100),“Y:+moveJoy.GetComponent().position.Y.ToString());
}
}

问题已经回答了。现在,如何使用这个脚本->创建一个球体,并将其命名为“球体碰撞器”、“网格渲染器”、“刚体”、“玩家控制(脚本)”在玩家控制脚本下,将这个脚本和您的操作完成。现在你有了一个可以在ios、android和pc上控制的球,我想你可以跳了。

我想canJump的旗帜上写着“地面上的球员”。所以,如果你“不能跳跃”,那就意味着你不应该让玩家在角色飞行时控制它。在Update()的最开始处检查它并调用return;如果canJump==false

很抱歉,我的编程能力不太好。是否要我在“void Update(){”下添加“return;if canJump==false”