简体   繁体   English

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

[英]jittery movement while jumping unity 2d

i am using a 2 scripts, a player movement and a camera follow script.我正在使用 2 个脚本,一个玩家移动和一个相机跟随脚本。 while jumping normally the jump is smooth, it is also smooth while moving my character.正常跳跃时跳跃很流畅,移动我的角色时也很流畅。 what i understand is that the camera follow script is somehow not allowing the jump to be smooth.here is the player movement script.我的理解是相机跟随脚本在某种程度上不允许跳转是平滑的。这里是玩家移动脚本。

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();
}
}

and here is the camera follow script.这是相机跟随脚本。

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));
}
}

can anyone help me with this one.谁能帮我解决这个问题。

FixedUpdate is not called every frame but in fixed intervals instead. FixedUpdate 不是每帧都调用,而是以固定的间隔调用。 It's more for physics and gameplay code.它更多地用于物理和游戏代码。

Try changing the camera follow script to Update.尝试将相机跟随脚本更改为更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM