Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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
C# 制作了一个类似地铁冲浪者的游戏,但角色在车道移动时结巴/振动_C#_Unity3d - Fatal编程技术网

C# 制作了一个类似地铁冲浪者的游戏,但角色在车道移动时结巴/振动

C# 制作了一个类似地铁冲浪者的游戏,但角色在车道移动时结巴/振动,c#,unity3d,C#,Unity3d,我做了一个像地铁冲浪者一样的游戏。游戏在单位编辑器上运行良好,但当我将其导出到apk并在移动设备上玩时,角色在我左右移动时会振动。 我没有添加任何刚体,而是添加了一个角色控制器 这是玩家移动代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PLAYERLANEMOV : MonoBehaviour { public const float LAN

我做了一个像地铁冲浪者一样的游戏。游戏在单位编辑器上运行良好,但当我将其导出到apk并在移动设备上玩时,角色在我左右移动时会振动。 我没有添加任何刚体,而是添加了一个角色控制器

这是玩家移动代码:

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

public class PLAYERLANEMOV : MonoBehaviour
{
    public const float LANE_DISTANCE = 2.5f;
    private const float TURN_SPEED = 0.5f;

    //
    private bool isRunning = false;

    //animation 

    private Animator anim;
    // movement 
    private CharacterController controller;
    private float jumbForce = 10.0f;
    private float gravity = 12.0f;
    private float verticalVelocity;
   
    private int desiredLane = 1; // 0=left , 1 = center ,2 = right 

    //speed modifier
    private float originalSpeed = 7.0f;
    private float speed = 7.0f;
    private float speedIncreaseLastTick;
    private float speedIncreaseTime = 2.5f;
    private float speedIncreaseAmount = 0.1f;

    private void Start()
    {
        speed = originalSpeed;
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    private void LateUpdate()
    {

        if (!isRunning)
            return;

        if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
        {
            speedIncreaseLastTick = Time.time;
            speed += speedIncreaseAmount;
            GameManager.Instance.UpdateModifier(speed - originalSpeed);
        }


        if (MobileInput.Instance.SwipeLeft)
            MoveLane(false);
        if (MobileInput.Instance.SwipeRight)
            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;

        bool isGrounded = IsGrounded();
        anim.SetBool("Grounded", isGrounded);
        //gravity
        if (isGrounded)
        {
            verticalVelocity = 0f;
            

            if (MobileInput.Instance.SwipeUp)
            {
                //jumb
                anim.SetTrigger("Jump");
                verticalVelocity = jumbForce;
            }
            else if (MobileInput.Instance.SwipeDown)
            {
                //slide
                StartSliding();
                Invoke("StopSliding", 1.0f);
            }
        }

        else
        {
            verticalVelocity -= (gravity * Time.deltaTime);

            //fast falling
            if (MobileInput.Instance.SwipeDown)
            {
                verticalVelocity = -jumbForce;
            }
        }
        moveVector.y = verticalVelocity;
        moveVector.z = speed;

        controller.Move(moveVector * Time.deltaTime);

        //rotate to where he is going
       // Vector3 dir = controller.velocity;
       // if (dir != Vector3.zero)
       // {
      //      dir.y = 0;
      //      transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
     //  }
    }

    private void StartSliding()
    {
        anim.SetBool("Sliding", true);
        controller.height /= 2;
        controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
    }

    private void StopSliding()
    {
        anim.SetBool("Sliding", false);
        controller.height *= 2;
        controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
    }

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

    bool IsGrounded()
        {
            Ray groundRay = new Ray(
                new Vector3(
                    controller.bounds.center.x,
                    (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
                    controller.bounds.center.z),
                Vector3.down);
            Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);

        return Physics.Raycast(groundRay, 0.2f + 0.1f); ;

    }

    public void StartRunning()
    {
        isRunning = true;
        anim.SetTrigger("StartRunning");
    } 

    private void Crash()
    {
        anim.SetTrigger("Death");
        isRunning = false;
        GameManager.Instance.OnDeath();
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        switch (hit.gameObject.tag)
        {
            case "Obstacle":
                Crash();
                break;
        }
    }


}
   using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CAMERAFOLLO : MonoBehaviour
{
    public Transform lookAt;// our charcter we are looking at 
    public Vector3 offset = new Vector3( 3.0f, -3.0f);
    public Vector3 rotation = new Vector3(35, 0, 0);

    public bool IsMoving { set; get; }


    private void LateUpdate()
    {
        if (!IsMoving)
            return;

        Vector3 desiredPosition = lookAt.position + offset;
       // desiredPosition.x = 0;
        transform.position = Vector3.Lerp(transform.position, desiredPosition,0.1f);
        transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(rotation),Time.deltaTime);
    }
}

Camera
GameObject
口吃通常是由

通常,您应该在
LateUpdate
中移动相机,并在中执行与帧速率无关的操作,例如播放器的移动

尝试在
playerlamov
类中将
private void LateUpdate()
更改为
private void FixedUpdate()


不要忘记,当您处理不同的
Update
调用时,例如
void Update
void FixedUpdate
您应该为每个调用使用正确的时间间隔,因为它们在调用之间使用不同的时间步长。对于
void Update
这将是
Time.deltaTime
void FixedUpdate
这将是

Aravind,我将添加更多关于导出到APK时的移动差异的细节,以及关于游戏的更多细节,除了“像地铁冲浪者”。我还要提到所有东西的版本号,包括你正在运行的android版本,因为这可能会确定在本地和手机上运行游戏之间的问题。