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()
	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
			
发布了261 篇原创文章 · 获赞 134 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/hello_tute/article/details/103787691