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);
    }    
}
相关推荐
hhb_61810 小时前
Dylan 语言核心特性与工程实践深度解析
开发语言·c#
mxwin10 小时前
Unity URP 半透明阴影的局限性
unity·游戏引擎
空中海11 小时前
第四篇:Unity高级阶段(架构级开发能力)
unity·架构·游戏引擎
CSharp精选营11 小时前
最新.NET新手入门学习网站合集(2026更新版)
c#·学习资料·开发教程·.net 新手入门·开放资源·.net网站
小贺儿开发11 小时前
【MediaPipe】Unity3D 虚拟面具互动演示
unity·人机交互·shader·摄像头·面具·互动·脸部捕捉
DaLiangChen13 小时前
Unity URP 绘制参考网格 Shader 教程(抗锯齿 + 渐变淡出)
unity·游戏引擎
空中海13 小时前
第三篇:Unity进阶阶段(商业项目能力)
unity·游戏引擎
hhb_61814 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
beyond谚语15 小时前
反射、特性和依赖注入
c#
Tiger_shl16 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#