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);
    }    
}
相关推荐
tedcloud12321 小时前
ppt-master部署教程:快速搭建智能演示文稿系统
服务器·人工智能·系统架构·游戏引擎·powerpoint
祀爱21 小时前
Asp.net core+ Layui 项目中编辑按钮传递数据的方法
前端·c#·asp.net·layui
雪豹阿伟1 天前
C# —— 上位机行业解析与完整学习规划
c#·上位机
一念春风1 天前
记事本(C#)
开发语言·c#
wangl_921 天前
初探 C# 15 的 Union Types
java·开发语言·算法·c#·.net·.net core
雪豹阿伟1 天前
C# 与 .NET 生态系统概述
c#·上位机
puamac1 天前
c#打开cmd然后输入claude
stm32·单片机·c#
雪豹阿伟1 天前
1.C# —— 项目结构、变量、数据类型与枚举
c#·上位机
雪豹阿伟1 天前
C# —— Visual Studio 2026 安装教程
c#·上位机