godot 3d模型 移动教程和代码

分类栏目:Godot教程

67

如何在godot里面让3d模型移动?


首先在选项设置里面添加  wasd 移动按钮

然后在模型里面加入下面代码 即可实现人物移动


extends KinematicBody


# How fast the player moves in meters per second.
export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
export var fall_acceleration = 75

var velocity = Vector3.ZERO
# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.

func _physics_process(delta):
# We create a local variable to store the input direction.
var direction = Vector3.ZERO

# We check for each move input and update the direction accordingly.
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_down"):
direction.z += 1
if Input.is_action_pressed("move_up"):
direction.z -= 1

if direction != Vector3.ZERO:
direction = direction.normalized()
$Pivot.look_at(translation + direction, Vector3.UP)
velocity.x = direction.x * speed
velocity.z = direction.z * speed
# Vertical velocity
velocity.y -= fall_acceleration * delta
# Moving the character
velocity = move_and_slide(velocity, Vector3.UP)