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);
    }    
}
相关推荐
rockey6274 小时前
基于AScript的JavaScript脚本语言发布啦
javascript·c#·.net·js·script
LuoCore4 小时前
AForge.Video.FFMPEG.dll加载失败解决指南
c#
吴可可1235 小时前
C#元组解构实现变量交换
c#
HH‘HH7 小时前
Unity引擎界面各个功能面板详细介绍
unity·游戏引擎
℡枫叶℡7 小时前
Unity 2D资产命名常用缩写
unity·资产命名缩写
慧都小妮子8 小时前
SciChart WPF v9.0 更新详解:矢量场图表、堆叠箱线图与 MVVM 轴同步来了
c#·.net·wpf·数据可视化·图表控件·图表
丁小未10 小时前
Unity AssetBundle商业化项目资源方案
unity·游戏引擎·assetbundle·unity框架
鱼听禅13 小时前
C#学习笔记-Entity Framework Core基础操作学习
c#·orm·ef core
Z59981784113 小时前
c#软件开发学习笔记--分组、游标与临时表、分页
笔记·学习·c#
rick97714 小时前
用 C# 从零搭建本地知识库:RAG 系统完整实战
c#