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);
    }    
}
相关推荐
钰fly30 分钟前
工具块与vs的联合编程(豆包总结生成)
c#
c#上位机1 小时前
wpf之行为
c#·wpf
星夜泊客1 小时前
C# 基础:为什么类可以在静态方法中创建自己的实例?
开发语言·经验分享·笔记·unity·c#·游戏引擎
dzj20212 小时前
PointerEnter、PointerExit、PointerDown、PointerUp——鼠标点击物体,则开始旋转,鼠标离开或者松开物体,则停止旋转
unity·pointerdown·pointerup
kylezhao20193 小时前
深入浅出地理解 C# WPF 中的属性
hadoop·c#·wpf
心前阳光3 小时前
Unity 模拟父子关系
android·unity·游戏引擎
多多*4 小时前
2月3日面试题整理 字节跳动后端开发相关
android·java·开发语言·网络·jvm·adb·c#
一念春风4 小时前
C# 通用工具类代码
c#
海盗12345 小时前
WPF上位机组件开发-设备状态运行图基础版
开发语言·c#·wpf
浮生如梦_6 小时前
C# 窗体工厂类 - 简单工厂模式演示案例
计算机视觉·c#·视觉检测·简单工厂模式