我们在进行游戏开发的时候,脚本是必不可少的。而我们应该始终拒绝去重新制造轮子,一些比较基础的脚本如果直接使用或修改成型的脚本,会在开发中节约不少时间。以下分享一个控制Unity2D角色移动脚本,让大家能够少造轮子,高效开发。
拿走即可用代码见文章尾部
含跑跳动画及跳跃音效功能代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private SpriteRenderer sprite;
private Animator anim;
[SerializeField] private LayerMask jumpableGround;
private float dirX = 0f;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 7f;
private enum MovementState { Idle, Run, Jump, Fall }
[SerializeField] private AudioSource jumpSoundEffect;
private void Start()
{
rb= GetComponent<Rigidbody2D>();
coll = GetComponent&