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);
    }    
}
相关推荐
LcGero几秒前
游戏引擎Luanti的前世今生与技术解析
游戏引擎·lua·游戏开发·我的世界·luanti
少控科技1 分钟前
小数典应用:农场环境数据采集监控
开发语言·windows·c#
冰凌糕18 分钟前
Unity3D Shader UV 与纹理采样
unity
¥-oriented1 小时前
记录使用C#编程中遇到的一个小bug
c#·bug
唐青枫1 小时前
C#.NET MemoryMarshal 深入解析:零拷贝内存重解释、二进制读写与使用边界
c#·.net
郝学胜-神的一滴1 小时前
[简化版 GAMES 101] 计算机图形学 06:相机视图矩阵的由来
c++·线性代数·unity·矩阵·godot·图形渲染·unreal engine
成都易yisdong16 小时前
纬地、鸿业、海地、CASS等横断面数据互转工具V3.2——测绘与道路设计人员的效率神器
c#·visual studio code
AIKZX20 小时前
西门子博途 TIA Portal v18 中文版图文安装教程(超级详细)附下载链接
开发语言·c#·编辑器·idea
xiaoshuaishuai821 小时前
C# 数字资源分发
开发语言·c#
XiaoZhangGOGOGO1 天前
《剑来2》动画第二季点映百度网盘提取码_百度网盘提取码资源
unity