[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来控制跟随的速度


总结

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

相关推荐
智算菩萨1 小时前
【OpenGL】6 真实感光照渲染实战:Phong模型、材质系统与PBR基础
开发语言·python·游戏引擎·游戏程序·pygame·材质·opengl
心前阳光5 小时前
Unity之ScrollRect简易实现
unity·游戏引擎
KaGme13 小时前
生成3DGS场景在unity中的呈现
3d·unity·游戏引擎
zyh______1 天前
关于unity的序列化
unity·游戏引擎
weixin_409383121 天前
godot碰撞测试的学习
学习·游戏引擎·godot
郭逍遥1 天前
[Godot] JPS跳点寻路和RVO避障
算法·godot·启发式算法
电子云与长程纠缠1 天前
Godot学习06 - AnimationPlayer内置动画
学习·游戏引擎·godot
mxwin1 天前
Unity Shader 齐次坐标与透视除法理解 SV_POSITION 的 w 分量
unity·游戏引擎·shader
电子云与长程纠缠2 天前
Godot学习05 - 播放与分离FBX动画
学习·游戏引擎·godot
weixin_409383122 天前
godot等轴视角tilemaplayer的学习 isocheric的素材xy大小怎么调
学习·游戏引擎·godot