Unity【角色/摄像机移动控制】【1.角色移动】

本文主要总结实现角色移动的解决方案。
1. 创建脚本:PlayerController
2. 创建游戏角色Player,在Player下挂载PlayerController脚本
3. 把Camera挂载到Player的子物体中,调整视角,以实现相机跟随效果
3. PlayerController脚本代码如下:

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    // Start is called before the first frame update
    void Start()
    {
        Transform transform = GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {      
        Move();
    }
    
    private void Move()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        
        Vector3 moveingVec = new Vector3(horizontalInput, 0f, verticalInput) * Time.deltaTime * speed;
        transform.Translate(moveingVec);
    }    
}
相关推荐
Dr.勿忘14 分钟前
Unity一分钟思路---UI任务条:宝箱位置如何准确卡在百分比位置上
ui·unity·游戏程序·屏幕适配
csdn_aspnet16 分钟前
WPF 做一个简单的电子签名板(一)
c#·wpf
weixin_4242946717 分钟前
Unity 实现 ScrollBar 值变化控制 Panel 位置的方法
unity·游戏引擎
玖笙&18 分钟前
✨WPF编程进阶【7.2】:动画类型(附源码)
c++·c#·wpf·visual studio
她说彩礼65万38 分钟前
C# 容器实例生命周期
开发语言·c#
清风与日月12 小时前
c# 集成激光雷达(以思岚A1为例)
开发语言·c#
无极小卒12 小时前
如何在三维空间中生成任意方向的矩形内部点位坐标
开发语言·算法·c#
the白勺14 小时前
RabbitMQ-基础-总结
开发语言·c#
专注VB编程开发20年15 小时前
C#VB.NET中实现可靠的文件监控(新建、删除、改名、内容修改等事件的准确捕获)
spring·c#·.net·文件监控
在路上看风景15 小时前
## 2.2 状态同步
unity