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);
    }    
}
相关推荐
软件黑马王子44 分钟前
24.Editor 资源加载:主要作用和基本原理
开发语言·前端框架·c#
慧都小妮子1 小时前
用 Python 批量把 PDF 参考文献排成 MLA 格式:Spire.Doc for Python 实战
python·pdf·c#·spire.doc·mla格式·参考文献排版·pdf批量处理
WarrenMondeville2 小时前
AI复刻街头霸王到 Unity
unity·游戏引擎
UIU1144 小时前
补码运算与整数溢出(上
学习·c#·补码·补码运算
曹牧6 小时前
C#:模态对话框
开发语言·c#
咕白m6256 小时前
C# 如何限制 PDF 复制、打印权限?
c#·.net
yume_sibai6 小时前
正则表达式完全指南(基础语法 + 实战应用 + 性能优化 + 最佳实践)
开发语言·正则表达式·c#
Nil2087 小时前
leetcode 79单词搜索
开发语言·c#
神仙别闹7 小时前
基于C#实现(WinForm)文件管理系统
java·开发语言·c#
reasonsummer7 小时前
【办公类-146-07】20260906《总园大班6个班级四大教育》(优化版:标题excle+复制AI文字+Python占位符录入)
人工智能·python·c#