📜  玩家移动脚本 unity - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:38.547000             🧑  作者: Mango

代码示例4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Rigidbody rb;
    [SerializeField] float movementSpeed = 6f;
    [SerializeField] float jumpForce = 5f;
    
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent();  
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        rb.velocity = new Vector3(horizontalInput * movementSpeed, rb.velocity.y, verticalInput * movementSpeed);

        if (Input.GetButtonDown("Jump"))
        {
            rb.velocity =  new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
        }
    }
}