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);
    }    
}
相关推荐
ghost1431 小时前
C#学习第29天:表达式树(Expression Trees)
开发语言·c#
安木夕11 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
gregmankiw14 小时前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
XR-AI-JK14 小时前
Unity VR/MR开发-VR/开发SDK选型对比分析
unity·vr·mr
阿蒙Amon15 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
钢铁男儿18 小时前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
林鸿群19 小时前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
o0向阳而生0o19 小时前
63、.NET 异常处理
c#·.net·异常处理
SteveDraw1 天前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
心之所向,自强不息1 天前
【Unity Shader编程】之让画面动起来
unity·游戏引擎