首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运动向右运动,但不向左运动。

运动向右运动,但不向左运动。
EN

Stack Overflow用户
提问于 2022-06-12 10:52:34
回答 1查看 26关注 0票数 0

我有一个动作脚本,当我向右移动时,它是好的,但当我向右移动时,它会结巴。我试过了,没有摄像机移动同样的问题。我只是不明白为什么。这两个方向的代码是相同的。

这是它从其他脚本中获取变量的代码,但是除了变量,它们没有任何其他代码。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputManager : MonoBehaviour
{
    Rigidbody2D rb;
    float jumpForce;
    float downForce;
    float speed;
    float maxSpeed;
    float maxJumps;
    float currentJumps;
    float cutJumpHeight;
    float jumpBufferTimer;
    float jumpBuffer;
    float jumpRememberTimer;
    float jumpRemember;
    float x;
    float y;
    bool isGrounded = false;
    AudioSource jumpSound;
    Jump jumpScript;
    Movement movementScript;
    GameObject[] groundCheckPoints;


    void Start()
    {
        jumpScript = GetComponent<Jump>();
        movementScript = GetComponent<Movement>();
        rb = GetComponent<Rigidbody2D>();
        groundCheckPoints = GameObject.FindGameObjectsWithTag("Check Point");
    }

    void Update()
    {
        MyInput();
        IsGrounded();
        FlipCharacter();
        jumpForce = jumpScript.jumpForce;
        speed = movementScript.speed;
        maxJumps = jumpScript.maxJumps;
        cutJumpHeight = jumpScript.cutJumpHeight;
        jumpRemember = jumpScript.jumpRememberTime;
        jumpBuffer = jumpScript.jumpBufferTime;
        jumpSound = jumpScript.jumpSound;
    }

    void FlipCharacter()
    {
        if (x > 0)
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }else if(x < 0)
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);
        }
    }

    void IsGrounded()
    {
        for (int i = 0; i < groundCheckPoints.Length; i++)
        {
            isGrounded = Physics2D.OverlapCircle(groundCheckPoints[i].transform.position, 0.1f, LayerMask.GetMask("Ground"));
            if (isGrounded)
            {
                break;
            }
        }
        Jump();
    }

    void FixedUpdate()
    {
            MovementPlatformer();
    }

    void MyInput()
    {
            x = Input.GetAxisRaw("Horizontal");
            y = Input.GetAxisRaw("Vertical");
    }

    void Jump()
    {
        jumpBufferTimer -= Time.deltaTime;
        jumpRememberTimer -= Time.deltaTime;

        if (isGrounded)
        {
            currentJumps = maxJumps;
        }

        if (currentJumps > 0)
        {
            jumpBufferTimer = jumpBuffer;
        }
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpRememberTimer = jumpRemember;
        }

        if (jumpBufferTimer > 0 && jumpRememberTimer > 0)
        {
            jumpBufferTimer = 0;
            jumpRememberTimer = 0;
            jumpSound.Play();

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            currentJumps--;
        }


        if (Input.GetKeyUp(KeyCode.Space) && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * cutJumpHeight);
        }
    }

    void MovementPlatformer()
    {
        rb.velocity = new Vector2(speed * x, rb.velocity.y);
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-19 08:26:34

这与翻转字符函数有关,当我将它移动到固定的,更新它,修复它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72591521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档