Unity控制角色移动常用两种方法:
一、在update函数中用transform.Translate()。
代码示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerContral : MonoBehaviour
{
private float player_speed = 3.0f; //角色移动速度
public float horizontal; //按下A、D或左右键会从零开始慢慢到-1或1,用来使角色移动更加平滑
void Start()
{
}
void Update()
{
horizontal = Input.GetAxis("Horizontal");
transform.Translate(Vector2.right * player_speed * Time.deltaTime * horizontal); //乘Time.deltaTime可以让角色速度保持恒定,不会随帧数变化而变化(因为函数update每帧执行一次,所以如果不承deltaTime速度就会因帧数变化而变化)
}