Godot Engine:3D角色移动(走/跑/跳)(重力问题已更正)

实现了走/跑/跳

extends KinematicBody

class_name Player

const walk_speed = 5.0
const run_speed = 10.0

var gravity:Vector3 = Vector3.DOWN*12
var speed:float = walk_speed
var jump_speed:float = 6.0
var velocity : Vector3 = Vector3.ZERO
var jump : bool = false


func _ready():
	add_to_group("player")

func _physics_process(delta):
	velocity += gravity*delta
	get_input()
	velocity = move_and_slide(velocity,Vector3.UP)
	if jump and is_on_floor():
		velocity.y = jump_speed
	
func get_input():
	var vy = velocity.y
	velocity = Vector3.ZERO
	if Input.is_action_pressed("ui_run"):
		speed = run_speed
	if Input.is_action_just_released("ui_run"):
		speed = walk_speed
		
	if Input.is_action_pressed("ui_up"):
		velocity += Vector3.FORWARD
	if Input.is_action_pressed("ui_down"):
		velocity += Vector3.BACK
	if Input.is_action_pressed("ui_left"):
		velocity += Vector3.LEFT
	if Input.is_action_pressed("ui_right"):
		velocity += Vector3.RIGHT
	
	
	velocity = velocity.normalized()*speed
	if velocity != Vector3.ZERO:
		look_at(translation - velocity,Vector3.UP)
	velocity.y = vy
	#动作键和跳跃键
	if Input.is_action_just_pressed("ui_action"):
		print("action")
	jump = false
	if Input.is_action_just_pressed("ui_jump"):
		jump = true
			

你可能感兴趣的:(Godot笔记,#,Godot,实践,Godot,角色移动,游戏开发,GDScript,move_and_slide)