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);
    }    
}
相关推荐
棪燊33 分钟前
Unity的Game视图在Scale放大后无法拖动
unity·游戏引擎
weixin_423995002 小时前
unity 团结开发小游戏,加载AssetBundles
unity·游戏引擎
cyr___3 小时前
Unity教程(二十七)技能系统 黑洞技能(下)黑洞状态
学习·游戏·unity·游戏引擎
齐鲁大虾4 小时前
新人编程语言选择指南
javascript·c++·python·c#
加号34 小时前
【C#】 WebAPI 接口设计与实现指南
开发语言·c#
张老师带你学5 小时前
Unity 科幻武器系列
科技·游戏·unity·模型·游戏美术
unicrom_深圳市由你创科技5 小时前
上位机开发常用的语言 / 框架有哪些?
c++·python·c#
平行云6 小时前
虚拟直播混合式2D/3D应用程序实时云渲染推流解决方案
linux·unity·云原生·ue5·图形渲染·实时云渲染·像素流送
cyr___6 小时前
Unity教程(二十六)技能系统 黑洞技能(上)基础实现
学习·游戏·unity·游戏引擎
xiaoshuaishuai87 小时前
C# ZLibrary数字资源分发
开发语言·windows·c#