Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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_Game Physics - Fatal编程技术网

Unity3d 统一切换车道后,角色是否保持波动?

Unity3d 统一切换车道后,角色是否保持波动?,unity3d,game-physics,Unity3d,Game Physics,我正在制作类似地铁冲浪者的游戏,但在编写车道切换脚本后,一旦我切换车道,我的角色开始波动。我在谷歌上搜索了这个问题,并尝试了多种解决方案,但都没有成功。我做错了什么 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio; public class PlayerMotor : MonoBehaviour { private const

我正在制作类似地铁冲浪者的游戏,但在编写车道切换脚本后,一旦我切换车道,我的角色开始波动。我在谷歌上搜索了这个问题,并尝试了多种解决方案,但都没有成功。我做错了什么

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class PlayerMotor : MonoBehaviour {

    private const float LANE_DISTANCE = 3.0f;
    private const float TURN_SPEED = 0.05f;
    private int desiredLane = 1; // 0 is left 1 is middle and 2 is right
    private float gravity = 12.0f;
    //vertical velocity 0.0
    private float verticalVelocity;
    private Vector3 moveVector;


    private bool isRunning = false;
    private Animator anim;
    private float jumpForce = 6.0f;
    private CharacterController controller;


    private float animationDuration = 3.0f;
    private float startTime;
    private bool isDead = false;


    //Speed Modifier
    private float orignalSpeed = 8.0f;
    private float speed;
    private float speedIncreasLastTick;
    private float speedIncreaseTime = 2.5f;
    private float speedIncreaseAmount = 0.8f;


    //sounds
    public AudioClip slide;
    public AudioClip collide;
    //public AudioClip StartMenu;
    //public AudioClip RunningSound;

    void Start () {

        speed = orignalSpeed;
        controller = GetComponent<CharacterController> ();
        startTime = Time.time;

    }


    void Update () {


        if(Time.time - speedIncreasLastTick > speedIncreaseTime)
        {
            speedIncreasLastTick = Time.time;
            speed += speedIncreaseAmount;

        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
            MoveLane (false);
        if (Input.GetKeyDown(KeyCode.RightArrow))
            MoveLane (true);

        Vector3 targetPosition = transform.position.z * Vector3.forward;
        if (desiredLane == 0)
            targetPosition += Vector3.left * LANE_DISTANCE;
        else if (desiredLane == 2)
            targetPosition += Vector3.right * LANE_DISTANCE;

        Vector3 moveVector = Vector3.zero;
        moveVector.x = (targetPosition - transform.position).normalized.x * speed;





        moveVector.y = verticalVelocity;
        moveVector.z = speed;


        //Move charachter

        controller.Move(moveVector * Time.deltaTime);

        //Rotate the character
        Vector3 dir = controller.velocity;
        if (dir != Vector3.zero) {
            dir.y = 0;
            transform.forward = Vector3.Lerp (transform.forward, dir, TURN_SPEED);
        }


    }

    public void SetSpeed(float modifier)
    {
        speed = 5.0f + modifier;
    }




    private void MoveLane(bool goingRight)
    {
        desiredLane += (goingRight) ? 1 : -1;
        desiredLane = Mathf.Clamp (desiredLane, 0, 2);
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Audio;
公共类游戏机:单一行为{
私人固定浮动车道距离=3.0f;
私用恒流浮动转弯速度=0.05f;
private int desiredLane=1;//0为左1为中2为右
私人浮子重力=12.0f;
//垂直速度0.0
私人流动性;
私有向量3移动向量;
私有布尔值正在运行=false;
私人动画师;
专用浮子跳线力=6.0f;
专用字符控制器;
私有浮动动画持续时间=3.0f;
私人浮动开始时间;
private bool isDead=false;
//速度调整器
私人浮动原始速度=8.0f;
私人浮动速度;
私人浮动速度递增lasttick;
私人浮动速度增加时间=2.5f;
私人浮动速度增加额=0.8f;
//听起来
公共音频剪辑幻灯片;
公共音频剪辑冲突;
//公共音频剪辑开始菜单;
//公共音频剪辑RunningSound;
无效开始(){
速度=原始速度;
controller=GetComponent();
startTime=Time.Time;
}
无效更新(){
如果(Time.Time-speedIncreaseLasttick>speedIncreaseTime)
{
speedIncreasLastTick=Time.Time;
速度+=速度增加量;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
MoveLane(假);
if(Input.GetKeyDown(KeyCode.RightArrow))
MoveLane(真);
Vector3 targetPosition=transform.position.z*Vector3.forward;
如果(desiredLane==0)
targetPosition+=矢量3.左*车道距离;
else if(desiredLane==2)
targetPosition+=矢量3.右*车道距离;
Vector3 movevevector=Vector3.0;
moveVector.x=(targetPosition-transform.position).normalized.x*速度;
moveVector.y=垂直性;
moveVector.z=速度;
//移动字符
controller.Move(movevevector*Time.deltaTime);
//旋转角色
Vector3 dir=控制器速度;
if(dir!=Vector3.zero){
方向y=0;
transform.forward=Vector3.Lerp(transform.forward,dir,TURN\u速度);
}
}
公共无效设置速度(浮动修改器)
{
速度=5.0f+修改器;
}
私人空地移动车道(布尔右侧)
{
所需车道+=(右转)?1:-1;
desiredLane=Mathf.夹具(desiredLane,0,2);
}
}
角色的位置.x和旋转应该为零,但在切换车道后,它开始波动


这不是因为角色的旋转吗?为什么需要旋转它?SwiftingDuster我将角色稍微旋转到他切换车道的一侧,但后来我也删除了旋转脚本,但问题仍然存在。