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);
    }    
}
相关推荐
xiaoshuaishuai83 小时前
C# 多线程之间对比
java·开发语言·c#
ysn111117 小时前
搭建状态同步框架的实践心得
unity·架构
一线灵9 小时前
Axmol:小众引擎的硬核逆袭
游戏引擎
z落落10 小时前
C# 多接口实现、重名成员、显式实现、接口继承+抽象类和接口区别
java·开发语言·c#
weixin_4419400110 小时前
【Unity教程】使用vuforia创建简单的AR实例
unity·游戏引擎·ar
郝学胜-神的一滴10 小时前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal
咸鱼翻身小阿橙10 小时前
高斯模糊降噪/磨皮算法降噪图像
前端·opencv·算法·webpack·c#
归真仙人20 小时前
【UE】LineTraceByProfile
ue5·游戏引擎·ue4·unreal engine
Song_da_da_21 小时前
C#与VisionPro联合编程实战:机器视觉二次开发完整指南
开发语言·microsoft·c#
加号31 天前
【C#】 Web API 自定义配置函数请求路径:从路由本质到灵活架构设计
开发语言·c#