【问题标题】:player controller jitters unity播放器控制器抖动团结
【发布时间】:2021-02-05 00:13:12
【问题描述】:

当我跳到一个立方体物体上时,我达到了步距偏移,它在再次落到地上之前会抖动一点。我可以完全消除步距偏移,但这不是我想要的,因为我的游戏是围绕跑酷进行的。当我做这个时,我正在关注 YouTube 上的 Brackeys 教程。 Brackeys tutorial. 谁能帮帮我? ,first person object , the object causing most problems, objects in scene

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

public class keymovement : MonoBehaviour
{
public CharacterController controller;


public float speed = 12f;
public float gravity = -10f;

public Transform groundcheck;
public float groundDistance = 0.5f;
public LayerMask groundMask;

public float jumpheight = 3f;

Vector3 velocity; 
bool isgrounded;


void Update()
{
    isgrounded = Physics.CheckSphere(groundcheck.position, groundDistance, groundMask);

   

    if (isgrounded && velocity.y < 0)

    {
        velocity.y = -2;
  }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;
    
    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButton("jump") && isgrounded)
    {
        velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }

      
    }
    }
    }

【问题讨论】:

  • 我没有添加刚体

标签: c# unity3d


【解决方案1】:

基本上,角色控制器正在与移动脚本的速度作斗争,因为它检查坡度和台阶的方式。

一个简单的解决方法是在跳跃时将控制器的slopeLimit 设置为90f

void Update()
{
    isgrounded = Physics.CheckSphere(groundcheck.position, groundDistance, groundMask);

    if (isgrounded && velocity.y < 0)

    {
        velocity.y = -2;
        controller.slopeLimit = 45f;
    }
    
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButton("Jump") && isgrounded)
    {
        velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
        controller.slopeLimit = 90f;
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
}

【讨论】:

  • 非常感谢,这真的很有帮助而且非常有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多