sign
2026/4/14大约 4 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — sign
sign
定义
sign() 是一个符号函数。它的作用极其简单:看一个数字是正数、负数还是零,然后分别返回 1、-1 或 0。它完全不关心数字的大小,只关心"方向"。
打个比方:想象你站在一条数轴上,sign() 就像一个路标指示器——它只告诉你"该往哪个方向走",而不关心"要走多远":
- 数字在原点右边(正数),路标指右(返回 1)
- 数字在原点左边(负数),路标指左(返回 -1)
- 数字在原点上(零),路标不动(返回 0)
再比如:你的银行账户余额是 +500 或 -300,sign() 只关心你是"赚了"还是"亏了",具体金额它不在乎。
函数签名
C#
// Mathf.Sign 接受 float,返回 int
public static int Sign(float s)
// Math.Sign 有多种重载
public static int Sign(int value)
public static int Sign(float value)
public static int Sign(double value)GDScript
func sign(x: float) -> float参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| x / s | float | 是 | 需要判断符号的数值 |
返回值
类型: C# 返回 int,GDScript 返回 float
| 输入条件 | 返回值 | 说明 |
|---|---|---|
x > 0(正数) | 1 / 1.0 | "正方向" |
x < 0(负数) | -1 / -1.0 | "负方向" |
x == 0(零) | 0 / 0.0 | "原地不动" |
代码示例
基础用法
C#
using Godot;
public partial class SignExample : Node
{
public override void _Ready()
{
// 正数返回 1
GD.Print(Mathf.Sign(5.0f)); // 运行结果: 1
GD.Print(Mathf.Sign(0.001f)); // 运行结果: 1
// 负数返回 -1
GD.Print(Mathf.Sign(-3.7f)); // 运行结果: -1
GD.Print(Mathf.Sign(-100.0f)); // 运行结果: -1
// 零返回 0
GD.Print(Mathf.Sign(0.0f)); // 运行结果: 0
}
}GDScript
func _ready():
# 正数返回 1.0
print(sign(5.0)) # 运行结果: 1.0
print(sign(0.001)) # 运行结果: 1.0
# 负数返回 -1.0
print(sign(-3.7)) # 运行结果: -1.0
print(sign(-100.0)) # 运行结果: -1.0
# 零返回 0.0
print(sign(0.0)) # 运行结果: 0.0实际场景:判断角色朝向方向
C#
using Godot;
public partial class PlayerDirection : CharacterBody2D
{
// 导出属性:角色移动速度
[Export] public float ExMoveSpeed = 200f;
// 内部变量:角色精灵节点
private Sprite2D _sprite;
public override void _Ready()
{
_sprite = GetNode<Sprite2D>("Sprite2D");
}
public override void _Process(double delta)
{
float horizontalInput = Input.GetAxis("move_left", "move_right");
// 用 sign 判断方向,只关心朝左还是朝右
int direction = Mathf.Sign(horizontalInput);
if (direction != 0)
{
// 翻转精灵:sign 返回 -1 时朝左,返回 1 时朝右
_sprite.FlipH = direction == -1;
float velocity = direction * ExMoveSpeed;
GD.Print($"移动方向: {direction}, 速度: {velocity}");
}
}
}GDScript
extends CharacterBody2D
# 导出属性:角色移动速度
@export var ex_move_speed: float = 200.0
# 内部变量:角色精灵节点
@onready var _sprite: Sprite2D = $Sprite2D
func _process(delta):
var horizontal_input = Input.get_axis("move_left", "move_right")
# 用 sign 判断方向,只关心朝左还是朝右
var direction = sign(horizontal_input)
if direction != 0:
# 翻转精灵:sign 返回 -1 时朝左,返回 1 时朝右
_sprite.flip_h = direction == -1
var velocity = direction * ex_move_speed
print("移动方向: %s, 速度: %s" % [direction, velocity])进阶用法:判断数值变化趋势与弹跳方向
C#
using Godot;
public partial class SignAdvanced : Node
{
public override void _Ready()
{
// 场景一:判断数值变化趋势
int oldScore = 100;
int newScore = 150;
int trend = Mathf.Sign(newScore - oldScore);
string trendText = trend switch
{
1 => "上升",
-1 => "下降",
_ => "持平"
};
GD.Print($"分数趋势: {trendText}");
// 运行结果: 分数趋势: 上升
// 场景二:弹跳方向——用 sign 保证速度的方向
float currentSpeed = -3.5f;
float bounceMultiplier = 0.8f;
// sign 保留方向,Abs 计算新速度大小
float newSpeed = Mathf.Sign(currentSpeed) * Mathf.Abs(currentSpeed) * bounceMultiplier;
GD.Print($"弹跳前速度: {currentSpeed}, 弹跳后速度: {newSpeed}");
// 运行结果: 弹跳前速度: -3.5, 弹跳后速度: -2.8
}
}GDScript
extends Node
func _ready():
# 场景一:判断数值变化趋势
var old_score = 100
var new_score = 150
var trend = sign(float(new_score - old_score))
var trend_text = "持平"
if trend > 0:
trend_text = "上升"
elif trend < 0:
trend_text = "下降"
print("分数趋势: %s" % trend_text)
# 运行结果: 分数趋势: 上升
# 场景二:弹跳方向——用 sign 保证速度的方向
var current_speed = -3.5
var bounce_multiplier = 0.8
# sign 保留方向,abs 计算新速度大小
var new_speed = sign(current_speed) * abs(current_speed) * bounce_multiplier
print("弹跳前速度: %s, 弹跳后速度: %s" % [current_speed, new_speed])
# 运行结果: 弹跳前速度: -3.5, 弹跳后速度: -2.8注意事项
只返回三个值:
sign()的返回值只有 1、-1、0 三种可能,你不会得到其他任何数字。不改变绝对值:
sign(100)和sign(1)都返回 1。它只关心"正负",完全不关心"大小"。C# 返回 int,GDScript 返回 float:在 C# 中
Mathf.Sign(5.0f)返回整数1,而在 GDScript 中sign(5.0)返回浮点数1.0。如果你在 C# 中需要浮点结果,需要手动转换:(float)Mathf.Sign(x)。浮点数精度问题:理论上为零的值可能因为浮点精度变成极小的正数或负数,导致
sign()返回 1 或 -1 而不是 0。如果你需要处理"接近零"的情况,可以配合is_zero_approx()使用。相关函数:如果你需要明确指定输入/输出类型,参见 signf(处理 float 返回 float)和 signi(处理 int 返回 int)。
