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);
    }    
}
相关推荐
离陌在学C#9 小时前
C# 事件(Event)详解:从概念到实战
开发语言·c#
rockey62711 小时前
C#脚本引擎之AScript与Flee对比
c#·.net·script·eval·动态脚本
zjnlswd17 小时前
C#学习笔记4
笔记·学习·c#
曹牧18 小时前
C#:List<string>特定格式输出
windows·c#
Z59981784119 小时前
c#软件开发学习笔记--Modbus-TCP/UDP网口通讯
笔记·学习·c#
曹牧20 小时前
C#:取的多个药品批准文号中的一个
c#
鹿野素材屋1 天前
Unity超轻量级中文语音播报,仅5兆大小,无需联网即可使用,适用于弹幕、提示等动态语音播出
unity·游戏引擎
用户298698530141 天前
3 种方法,轻松将 PowerPoint 转换为 PDF 格式
人工智能·后端·c#
KindSuper_liu1 天前
HybridCLR DHE及MetaVersion工作流核心概念
c#
淡海水1 天前
01-08-运行时-虚方法分派与接口调用的底层实现
linux·windows·microsoft·c#·虚方法·vtable·vsd