1重力组件
为player添加组件例子如下,添加重力组件
2.输入系统
选择Unity自带的快捷添加输入系统的方法
给player添加组件时选择Player Input,跟着默认操作进行。
勾选Generate C# Class,点击应用。
3.基础移动脚本的编写
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayControl : MonoBehaviour
{
public PlayerInputControl inputcontral;
public Vector2 inputDirection;
private Rigidbody2D rb;
public float speed;
private void Awake()
{
inputcontral = new PlayerInputControl();
rb = GetComponent<Rigidbody2D>();
}
private void OnEnable()
{
inputcontral.Enable();
}
private void OnDisable()
{
inputcontral.Disable();
}
private void Update()
{
inputDirection = inputcontral.Player.Move.ReadValue<Vector2>();
}
private void FixedUpdate()
{
Move();
}
public void Move(){
rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, rb.velocity.y);
int faceDir = (int)transform.localScale.x;
if(inputDirection.x > 0)
faceDir = 1;
if(inputDirection.x < 0)
faceDir = -1;
//人物翻转
transform.localScale = new Vector3(faceDir,1,1);
}
}