跳跃统一2d时的抖动运动



我使用的是2个脚本、一个玩家移动和一个相机跟随脚本。在正常跳跃时,跳跃是平稳的,在移动我的角色时也是平稳的。我所理解的是,摄影机跟随脚本在某种程度上不允许跳跃流畅。这是玩家移动脚本。

public class player_movement : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float jumpForce;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
movement();
}
private void movement()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (isGrounded == false && moveInput == 0)
{
anim.SetBool("is running", false);
}
else if (isGrounded == true && moveInput != 0)
{
anim.SetBool("is running", true);
}
else if(isGrounded == true && moveInput == 0)
{
anim.SetBool("is running", false);
}
else if (isGrounded == false && moveInput != 0)
{
anim.SetBool("is running", false);
}
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector2(0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector2(0, 180);
}
Jump();
}
private void Jump()
{
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}


if (Input.GetKey(KeyCode.Space))
{
if (jumpTimeCounter > 0 && isJumping == true)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
private void LateUpdate()
{
//Jump();
}
}

这是摄像机的跟踪脚本。

public class cammeraFollow : MonoBehaviour
{
public GameObject followObject;
public Vector2 followOffset;
public float speed = 3f;
private Vector2 threshold;
private Rigidbody2D rb;
void Start()
{
threshold = calculateThreshold();
rb = followObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 follow = followObject.transform.position;
float xDifference = Vector2.Distance(Vector2.right * transform.position.x, Vector2.right * follow.x);
float yDifference = Vector2.Distance(Vector2.up * transform.position.y, Vector2.up * follow.y);
Vector3 newPosition = transform.position;
if(Mathf.Abs(xDifference) >= threshold.x)
{
newPosition.x = follow.x;
}
if (Mathf.Abs(yDifference) >= threshold.y)
{
newPosition.y = follow.y;
}
float moveSpeed = rb.velocity.magnitude > speed ? rb.velocity.magnitude : speed;
transform.position = Vector3.MoveTowards(transform.position, newPosition, moveSpeed * Time.deltaTime);
}
private Vector3 calculateThreshold()
{
Rect aspect = Camera.main.pixelRect;
Vector2 t = new Vector2(Camera.main.orthographicSize * aspect.width / aspect.height, Camera.main.orthographicSize);
t.x -= followOffset.x;
t.y -= followOffset.y;
return t;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Vector2 border = calculateThreshold();
Gizmos.DrawWireCube(transform.position, new Vector3(border.x * 2, border.y * 2, 1));
}
}

有人能帮我做这个吗。

不是每帧都调用FixedUpdate,而是以固定的间隔调用。它更多的是物理和游戏代码。

尝试将相机跟随脚本更改为"更新"。

最新更新