[Godot] C#简单实现人物的控制和动画

目录

实现效果

场景搭建

脚本实现

移动

动画

完整脚本

相机跟随

总结


实现效果


场景搭建

本文章只分享了关于移动和动画的,没有给碰撞体,大家根据需要自行添加吧

相机的缩放大小可以根据自己的需要调整

我的人物动画结构是这样的,待机动(idle )}和移动动画(walk


脚本实现

移动

首先,我们需要声明一个移动速度变量,然后将移动代码写在Process函数中

cs 复制代码
using Godot;
using System;

public partial class Player : CharacterBody2D
{
    [Export] public float moveSpeed;

    public override void _Process(double delta)
    {
        Move((float)delta);
    }

    void Move(float delta)
    {
        Vector2 direction = Vector2.Zero;

        if (Input.IsActionPressed("right"))
            direction.X += 1;
        if (Input.IsActionPressed("left"))
            direction.X -= 1;
        if (Input.IsActionPressed("down"))
            direction.Y += 1;
        if (Input.IsActionPressed("up"))
            direction.Y -= 1;

        direction = direction.Normalized();
        Velocity = direction * moveSpeed;
        MoveAndSlide(); //根据速度滑动式移动角色,并自动处理碰撞
    }

}

构建场景,在人物面板设置变量值,然后在面板里面添加按键映射,我们可以就能正常的移动人物了

动画

接下来是动画部分,我用枚举实现了一个简单的状态机,并且在移动代码里面添加了水平翻转的代码

cs 复制代码
using Godot;
using System;

public partial class Player : CharacterBody2D
{
    private enum AnimState
    {
        idle,
        walk
    }
    AnimState animState = AnimState.idle;

    private AnimatedSprite2D _playerAnim;

    public override void _Ready()
    {
        _playerAnim = GetNode<AnimatedSprite2D>("./AnimatedSprite2D");
    }

    public override void _Process(double delta)
    {
        Move((float)delta);

        AnimStateSwitch();
    }

    void Move(float delta)
    {
        ...

        if (direction != Vector2.Zero)
        {
            ...

            if (direction.X > 0) _playerAnim.FlipH = false;
            else if (direction.X < 0) _playerAnim.FlipH = true;
        }
        else
        {
            animState = AnimState.idle;
        }
    }

    void AnimStateSwitch()
    {
        switch (animState)
        {
            case AnimState.idle: _playerAnim.Play("idle"); break;
            case AnimState.walk: _playerAnim.Play("walk"); break;
        }
    }
}

构建并运行场景,我们就能丝滑的控制人物的动画了

完整脚本

cs 复制代码
using Godot;
using System;

public partial class Player : CharacterBody2D
{
    [Export] public float moveSpeed;

    private enum AnimState
    {
        idle,
        walk
    }
    AnimState animState = AnimState.idle;

    private AnimatedSprite2D _playerAnim;

    public override void _Ready()
    {
        _playerAnim = GetNode<AnimatedSprite2D>("./AnimatedSprite2D");
    }

    public override void _Process(double delta)
    {
        Move((float)delta);

        AnimStateSwitch();
    }

    void Move(float delta)
    {
        Vector2 direction = Vector2.Zero;

        if (Input.IsActionPressed("right"))
            direction.X += 1;
        if (Input.IsActionPressed("left"))
            direction.X -= 1;
        if (Input.IsActionPressed("down"))
            direction.Y += 1;
        if (Input.IsActionPressed("up"))
            direction.Y -= 1;

        direction = direction.Normalized();
        if (direction != Vector2.Zero)
        {
            Velocity = direction * moveSpeed;
            animState = AnimState.walk;
            MoveAndSlide();

            if (direction.X > 0) _playerAnim.FlipH = false;
            else if (direction.X < 0) _playerAnim.FlipH = true;
        }
        else
        {
            animState = AnimState.idle;
        }
    }

    void AnimStateSwitch()
    {
        switch (animState)
        {
            case AnimState.idle: _playerAnim.Play("idle"); break;
            case AnimState.walk: _playerAnim.Play("walk"); break;
        }
    }
}

相机跟随

关于相机的平滑跟随,我们有一个简单的办法,设置Position Smoothing 属性EnabledTrue 即可平滑跟随,可以设置speed来控制跟随的速度


总结

这里简单的跟大家分享了一下基础的控制人物方法,大家可以根据需要自行调整

相关推荐
m0_497214154 小时前
unity中通过拖拽,自定义scroll view中子物体顺序
unity·游戏引擎
郝学胜-神的一滴1 天前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
EQ-雪梨蛋花汤2 天前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎
极客柒3 天前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
一线灵3 天前
跨平台游戏引擎 Axmol-2.8.1 发布
junit·游戏引擎
王家视频教程图书馆3 天前
2025年最新 unityHub游戏引擎开发2d手机游戏和桌面游戏教程
游戏·unity·游戏引擎
Xeon_CC5 天前
Unity中,软遮罩SoftMaskForUGUI的使用
unity·游戏引擎
DanmF--6 天前
NGUI--三大基础组件
unity·游戏引擎
Xeon_CC6 天前
Unity中,软遮罩SoftMaskForUGUI可移动遮罩形状实现方法
unity·游戏引擎
Yasin Chen6 天前
Unity Standard Shader 解析(五)之ShadowCaster
unity·游戏引擎