【发布时间】:2020-06-13 16:09:24
【问题描述】:
我对 Unity 和 C# 很陌生。我正在尝试开发我的第一个游戏,很多语法让我感到困惑。当按下“a”或“d”或箭头键时,我正在尝试使用力在 x 轴上移动我的精灵,但我的精灵似乎没有以恒定的速度移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playerMovement : MonoBehaviour
{
public float vel = .5f;
public float jumpVel = 10f;
public bool isGrounded = false;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = transform.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f,0f);
//transform.position += move * Time.deltaTime * vel;
if(Input.GetAxisRaw("Horizontal") == 1){
rb.AddForce(Vector2.right*vel, ForceMode2D.Force);
}else if(Input.GetAxisRaw("Horizontal") == -1){
rb.AddForce(Vector2.right*-vel, ForceMode2D.Force);
}
if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true){
rb.velocity = Vector2.up * jumpVel;
}
}
}
【问题讨论】: