玩家使用Unity3D中的角色控制器像地铁冲浪者一样滑动运动控制器?

玩家使用Unity3D中的角色控制器像地铁冲浪者一样滑动运动控制器?,unity3d,Unity3d,我试图使一个球员的运动控制器,如地铁冲浪者使用字符控制器,每件事都与键盘工作良好,但我得到了一个问题,在刷卡。当我滑动它时,它只移动一帧。我也希望球员在空中时左右移动(跳跃)。请帮忙 这是我的密码: using UnityEngine; using System.Collections; public class PlayerControllerScript : MonoBehaviour { public float speed = 8.0F; public float ju

我试图使一个球员的运动控制器,如地铁冲浪者使用字符控制器,每件事都与键盘工作良好,但我得到了一个问题,在刷卡。当我滑动它时,它只移动一帧。我也希望球员在空中时左右移动(跳跃)。请帮忙

这是我的密码:

using UnityEngine;
using System.Collections;

public class PlayerControllerScript : MonoBehaviour
{
    public float speed = 8.0F;
    public float jumpSpeed = 16.0F;
    public float gravity = 80.0F;
    private Vector3 moveDirection = Vector3.zero;

    public int laneNumber = 1;
    public int lanesCount = 3;
    bool didChangeLastFrame = false;
    public float laneDistance = 2;
    public float firstLaneXPos = -2;
    public float deadZone = 0.1f;
    public float sideSpeed = 12;
    private bool Right = false;
    private bool Left = false;


    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();

        float input = Input.GetAxis("Horizontal");
        if (controller.isGrounded) {
            if (Mathf.Abs(input) > deadZone)
            {
                if (!didChangeLastFrame)
                {
                    didChangeLastFrame = true;
                    laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                    if (laneNumber < 0) laneNumber = 0;
                    else if (laneNumber >= lanesCount) laneNumber = lanesCount - 1;
                }
            }
            else
            {
                didChangeLastFrame = false;

                moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;

                if (Input.GetButton("Jump") || SwipeManager.IsSwipingUp())
                    moveDirection.y = jumpSpeed;
            }
        }

        if (Left)
            moveDirection.x = -jumpSpeed;

        if (Right)
            moveDirection.x = jumpSpeed;

        if (SwipeManager.IsSwipingLeft())
        {
            Left = true;
            Right = false;
        }

        if (SwipeManager.IsSwipingRight())
        {
            Right = true;
            Left = false;
        }

        Vector3 pos = transform.position;
        pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
        transform.position = pos;

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}
使用UnityEngine;
使用系统集合;
公共类PlayerController脚本:MonoBehavior
{
公共浮子速度=8.0F;
公共浮子跳跃速度=16.0F;
公共浮子重力=80.0F;
专用矢量3移动方向=矢量3.0;
公共整数laneNumber=1;
公共int lanesCount=3;
bool didChangeLastFrame=false;
公共浮点数=2;
公共浮动利率=2;
公共浮动死区=0.1f;
公共浮子侧速度=12;
私有布尔右=假;
私有布尔左=假;
无效更新()
{
CharacterController=GetComponent();
float input=input.GetAxis(“水平”);
if(controller.isground){
if(数学Abs(输入)>死区)
{
如果(!didChangeLastFrame)
{
didChangeLastFrame=true;
laneNumber+=Mathf.RoundToInt(数学符号(输入));
如果(laneNumber<0)laneNumber=0;
如果(laneNumber>=lanesCount)laneNumber=lanesCount-1,则为else;
}
}
其他的
{
didChangeLastFrame=false;
moveDirection=新矢量3(0,0,Input.GetAxis(“垂直”);
moveDirection=transform.TransformDirection(moveDirection);
移动方向*=速度;
if(Input.GetButton(“Jump”)| | swipmanager.IsSwipingUp())
移动方向。y=跳跃速度;
}
}
如果(左)
moveDirection.x=-跳跃速度;
如果(右)
移动方向。x=跳跃速度;
if(swipmanager.IsSwipingLeft())
{
左=真;
右=假;
}
if(swipmanager.IsSwipingRight())
{
右=真;
左=假;
}
向量3位置=变换位置;
pos.x=数学Lerp(pos.x,firstLaneXPos+LaneInstance*laneNumber,Time.deltaTime*sideSpeed);
transform.position=pos;
moveDirection.y-=重力*时间增量;
控制器移动(移动方向*时间增量);
}
}
我想

if(Input.GetButton(“Jump”)| | swipmanager.IsSwipingUp())
移动方向。y=跳跃速度
必须在if(controller.isground)块之外。所以,不管重力如何,你都要跳。
这是最后的代码

using UnityEngine;
using System.Collections;

public class PlayerControllerScript : MonoBehaviour
{
public float speed = 8.0F;
public float jumpSpeed = 16.0F;
public float gravity = 80.0F;
private Vector3 moveDirection = Vector3.zero;

public int laneNumber = 1;
public int lanesCount = 3;
bool didChangeLastFrame = false;
public float laneDistance = 2;
public float firstLaneXPos = -2;
public float deadZone = 0.1f;
public float sideSpeed = 12;
private bool Right = false;
private bool Left = false;


void Update()
{
    CharacterController controller = GetComponent<CharacterController>();

    float input = Input.GetAxis("Horizontal");
    if (controller.isGrounded) {
        if (Mathf.Abs(input) > deadZone)
        {
            if (!didChangeLastFrame)
            {
                didChangeLastFrame = true;
                laneNumber += Mathf.RoundToInt(Mathf.Sign(input));
                if (laneNumber < 0) laneNumber = 0;
                else if (laneNumber >= lanesCount) laneNumber = lanesCount - 1;
            }
        }
        else
        {
            didChangeLastFrame = false;

            moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
        }
    }

    if (Input.GetButton("Jump") || SwipeManager.IsSwipingUp())
                moveDirection.y = jumpSpeed;

    if (Left)
        moveDirection.x = -jumpSpeed;

    if (Right)
        moveDirection.x = jumpSpeed;

    if (SwipeManager.IsSwipingLeft())
    {
        Left = true;
        Right = false;
    }

    if (SwipeManager.IsSwipingRight())
    {
        Right = true;
        Left = false;
    }

    Vector3 pos = transform.position;
    pos.x = Mathf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
    transform.position = pos;

    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}
}
使用UnityEngine;
使用系统集合;
公共类PlayerController脚本:MonoBehavior
{
公共浮子速度=8.0F;
公共浮子跳跃速度=16.0F;
公共浮子重力=80.0F;
专用矢量3移动方向=矢量3.0;
公共整数laneNumber=1;
公共int lanesCount=3;
bool didChangeLastFrame=false;
公共浮点数=2;
公共浮动利率=2;
公共浮动死区=0.1f;
公共浮子侧速度=12;
私有布尔右=假;
私有布尔左=假;
无效更新()
{
CharacterController=GetComponent();
float input=input.GetAxis(“水平”);
if(controller.isground){
if(数学Abs(输入)>死区)
{
如果(!didChangeLastFrame)
{
didChangeLastFrame=true;
laneNumber+=Mathf.RoundToInt(数学符号(输入));
如果(laneNumber<0)laneNumber=0;
如果(laneNumber>=lanesCount)laneNumber=lanesCount-1,则为else;
}
}
其他的
{
didChangeLastFrame=false;
moveDirection=新矢量3(0,0,Input.GetAxis(“垂直”);
moveDirection=transform.TransformDirection(moveDirection);
移动方向*=速度;
}
}
if(Input.GetButton(“Jump”)| | swipmanager.IsSwipingUp())
移动方向。y=跳跃速度;
如果(左)
moveDirection.x=-跳跃速度;
如果(右)
移动方向。x=跳跃速度;
if(swipmanager.IsSwipingLeft())
{
左=真;
右=假;
}
if(swipmanager.IsSwipingRight())
{
右=真;
左=假;
}
向量3位置=变换位置;
pos.x=数学Lerp(pos.x,firstLaneXPos+LaneInstance*laneNumber,Time.deltaTime*sideSpeed);
transform.position=pos;
moveDirection.y-=重力*时间增量;
控制器移动(移动方向*时间增量);
}
}

CharacterController
移动到
PlayerControllerScript
脚本的
private
字段,以获得更好的性能。因为它不会改变。并使
controller=GetComponent()
Start()
中。是的,你是对的,我把它移到了Start(),你能帮我回答我问的问题吗@卡米基特