具有字符控制器的字符非常抖动/抖动

时间:2019-05-13 15:52:51

标签: c# unity3d

我的角色有一个角色控制器,该控制器在移动时会非常抖动。我尝试调整字符控制器值,但对我没有任何帮助。这是一款无限的亚军游戏,玩家在站立的同时移动并执行certin动画时不会出现抖动,但在开始移动时会严重抖动。

public class PlayerMotor : MonoBehaviour
{
public static PlayerMotor Instance { get; set;}
private const float LANE_DISTANCE = 2.5f;
private const float TURN_SPEED = 0.05f;
public GameObject Enemy;
public GameObject StakeBoard;

private bool isRunning = false;
public bool magnetAttract = false;
public bool Skateboarding = false;
public bool SkateValue = true;

//Animation
[HideInInspector]
public Animator anim;

//Movement
private CharacterController controller;
private float jumpForce = 10.0F;   // how high player can jump
private float gravity = 12.0f;    //Gravity makes the body fall down
private float verticalVelocity;//This will be set for every single frame

private int desiredLane = 1;   // 0=Left , 1= Middle , 2=Right

//Speed Modifier
private float originalSpeed = 8.0f;
private float Speed = 8.0f;    // Player Speed
private float speedIncreaseLastTick;   // Last time speed was increased
private float speedIncreaseTime = 2.5f;
private float speedIncreaseAmount = 0.1f;



private void Start()
{

    Instance = this;

    Speed = originalSpeed;
    controller = GetComponent<CharacterController>();
    anim = GetComponentInChildren<Animator>();
}

private void Update()
{
    if (!isRunning)
    return;

if (Skateboarding == true && SkateValue==true)
    {
        anim.SetBool("Skate", true);
        StakeBoard.SetActive(true);
        SkateValue = false;
    }

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


    //Gather the inputs on which lane we should be
    if (MobileInput.Instance.SwipeLeft)
        MoveLane(false);
    if (MobileInput.Instance.SwipeRight)
        MoveLane(true);


    // Calculate Where we should be in the future
    Vector3 targetPosition = transform.position.z * Vector3.forward;
    if (desiredLane == 0)
        targetPosition += Vector3.left * LANE_DISTANCE;
    else if (desiredLane == 2)
        targetPosition += Vector3.right * LANE_DISTANCE;

    // Calculate our move dalta
    Vector3 moveVector = Vector3.zero;
    moveVector.x = (targetPosition - transform.position).normalized.x
* Speed;  //where we should be - where we are right now  
, directional vector


bool isGrounded = IsGrounded();
    anim.SetBool("Grounded", isGrounded);


    // Calculate Y
    if (IsGrounded()) //if Grounded
    {
        verticalVelocity = -0.1f;

        if (MobileInput.Instance.SwipeUp)
        {
            //Jump
            if (Skateboarding == true)
            {
                anim.SetBool("SkateJump", true);
            }else
            anim.SetBool("Jump", true);

            verticalVelocity = jumpForce;

            Invoke("Stopjump", 1.0f);

        }
        else if (MobileInput.Instance.SwipeDown)
        {
            //Slide
            StartSliding();
            Invoke("StopSliding", 1.0f);
            FindObjectOfType<AudioManager>().Play("slide");
        }

    }

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

        //Fast falling Mechanics while in the air
        if(MobileInput.Instance.SwipeDown)
        {
            verticalVelocity = -jumpForce;
        }

    }
    moveVector.y = verticalVelocity;
    moveVector.z = Speed;

    //Move the player
    controller.Move(moveVector * Time.deltaTime);

    //Rotate the player 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()
{
    if (Skateboarding == true)
    {
        anim.SetBool("SkateSlide", true);

    }
    else
        anim.SetBool("Sliding", true);

    controller.height /= 2;
    controller.center = new Vector3(controller.center.x, 
controller.center.y/2,controller.center.z);
}

private void StopSliding()
{
    if (Skateboarding == true)
    {
        anim.SetBool("SkateSlide", false);
    }
    else
        anim.SetBool("Sliding", false);

    controller.height *= 2;
    controller.center = new Vector3(controller.center.x, 
controller.center.y * 2, controller.center.z);
}
private void Stopjump()
{
    if (Skateboarding == true)
    {
        anim.SetBool("SkateJump", false);
    }
    else
        anim.SetBool("Jump", false);
   // controller.height *= 2;
 //   controller.center = new Vector3(controller.center.x, 
controller.center.y * 2, controller.center.z);
}
public void MoveLane(bool goingRight)
{

    desiredLane += (goingRight) ? 1 : -1;
    desiredLane = Mathf.Clamp(desiredLane, 0, 2);

    // Same As above to check on which lane palyer is 
    //if (!goingRight)
    //{
    //    desiredLane--;
    //    if(desiredLane==-1)
    //        desiredLane = 0;
    //}
    //else
    //{
    //    desiredLane++;
    //    if(desiredLane==3)
    //        desiredLane = 2;
    //}
}

private bool IsGrounded()
{
    Ray groundray = new Ray(
    new Vector3(
    controller.bounds.center.x,
    (controller.bounds.center.y - controller.bounds.extents.y) + 0.02f,
    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");

  //  enemy.Instance.go();
    isRunning = false;
    GameManager.Instance.OnDeath();
    FindObjectOfType<AudioManager>().Play("dead");

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



}
private void OnTriggerEnter(Collider other)
{
    if (other.tag == "scene")
        GameManager.currentLocation.z = 0.0f;
    if(other.tag=="scene2")
        GameManager.currentLocation.z = 200.0f;
    if (other.tag=="scene3")
        GameManager.currentLocation.z = 400.0f;
    if (other.tag=="scene4")
        GameManager.currentLocation.z = 600.0f;
    if (other.tag=="scene5")
        GameManager.currentLocation.z = 800.0f;

}

}

0 个答案:

没有答案