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);
    }    
}
相关推荐
可爱系程序猿7 小时前
Unity 材质预览才弹 d3dcompiler_47.dll?先分清编辑器 SDK、着色器缓存和玩家包
程序人生·3d·unity·电脑·材质
xcLeigh8 小时前
Unity基础:使用Transform控制物体缩放——localScale与lossyScale的区别
unity·游戏引擎·教程
Chen—LSN10 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
郝学胜-神的一滴11 小时前
游戏引擎原理与实践 01:聊聊游戏引擎的前世今生
开发语言·c++·游戏引擎·产品运营·软件工程·产品经理·untiy
淡海水13 小时前
00-Unity和C#的GC剖析与对比-全篇导览
unity·c#·游戏引擎·gc
fogota14 小时前
C# `string.Join` 处理 `List<int>`
windows·c#
Behavior17 小时前
Unity 手游动态更换 App 图标 — Android 与 iOS 双端技术方案
c#·unity3d·游戏开发
周杰伦fans17 小时前
构建真正智能的CAD AI绘图代理
开发语言·人工智能·c#
UIU11420 小时前
递归算法与汉诺塔
c++·学习·算法·c#·递归
CSharp精选营1 天前
我用 ASP.NET Core 做了个水稻病虫害检查系统
c#·毕业设计·.net core·码农刚子