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);
    }    
}
相关推荐
两千次34 分钟前
图像的处理 图片裁剪工具方法 图片按比例缩放的工具方法
c#
Sunsets_Red1 小时前
浅谈随机化与模拟退火
java·c语言·c++·python·算法·c#·信息学竞赛
爱搞虚幻的阿恺1 小时前
Niagara粒子系统-超炫酷的闪电特效(第二期 旋转的纸片)
游戏·游戏引擎·虚幻
两千次3 小时前
图像的处理 图像转haclon
c#
WarPigs7 小时前
Unity渲染问题记录
unity·游戏引擎
bugcome_com7 小时前
C# 运算符详解:类型、实例及优先级
c#
不绝1917 小时前
导入3D模型时相关参数设置/Model标签页/Rig标签页/Avatar相关参数/Animation标签页/Materia标签页
unity·游戏程序
C#程序员一枚8 小时前
Web Service 和 Web API
c#
dlpay10 小时前
使用blender搭建模型并导入godot游戏引擎
游戏引擎·godot·blender