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);
    }    
}
相关推荐
福赖7 分钟前
《C#反射机制》
开发语言·c#
向上的车轮19 分钟前
熟悉C#如何转TypeScript?
开发语言·typescript·c#
我是唐青枫1 小时前
C#.NET ReaderWriterLockSlim 深入解析:读写锁原理、升级锁与使用边界
开发语言·c#·.net
WarrenMondeville1 小时前
4.Unity面向对象-接口隔离原则
java·unity·接口隔离原则
The Sheep 20231 小时前
C# 操作XML
xml·前端·c#
JosieBook2 小时前
【C#】C# 中的 enum、struct 和 class 对比总结
开发语言·算法·c#
WarrenMondeville2 小时前
3.Unity面向对象-里氏替换原则
unity·游戏引擎·里氏替换原则
Scout-leaf3 小时前
WPF新手村教程(七)—— 终章(MVVM架构初见杀)
c#·wpf
WarrenMondeville4 小时前
5.Unity面向对象-依赖倒置原则
unity·设计模式·依赖倒置原则
ZoeJoy84 小时前
机器视觉C# 调用相机:从 USB 摄像头到海康工业相机(WinForms & WPF)
数码相机·c#·wpf