我试图在Godot中给我的角色添加相机旋转,但我一直收到错误消息“在基本的‘Vector3’中对函数'rotated‘的无效调用。预期的2个参数”我尝试将方向变量设为向量2,但当我这样做时,使我的bean角色跳跃的物理函数将不起作用,因为必须有另一个轴可以在代码中移动:(错误来自第58行)
extends KinematicBody
var damage = 10
var speed = 500
var direction = Vector3()
var gravity = -9.8
var velocity = Vector3()
var sens = 0.2
onready var cam = get_node("Camera")
onready var anim_player = $AnimationPlayer
onready var camera = $Camera
onready var raycast = $Camera/RayCast
func _ready():
print("hi")
func _input(event):
if event is InputEventMouseMotion:
var movement = event.relative
cam.rotation.x += -deg2rad(movement.y * sens)
cam.rotation.x = clamp(cam.rotation.x, deg2rad(-45), deg2rad(180))
rotation.y += -deg2rad(movement.x * sens)
func fire():
if Input.is_action_pressed("Fire"):
if not anim_player.is_playing():
if raycast.is_colliding():
var target = raycast.get_collider()
if target.is_in_group("enemy"):
target.health -=damage
anim_player.play("AssaultRifleFire")
else:
anim_player.stop()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
fire()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if Input.is_key_pressed(KEY_ESCAPE):
get_tree().quit()
direction = direction.normalized().rotated(-rotation.y)
direction = Vector3(0, 0, 0)
if Input.is_action_pressed("Left"):
direction.z -= 1
if Input.is_action_pressed("Right"):
direction.z += 1
if Input.is_action_pressed("Forward"):
direction.x += 1
if Input.is_action_pressed("Back"):
direction.x -= 1
direction = direction.normalized()
direction = direction * speed * delta
if velocity.y > 0:
gravity = -20
else:
gravity = -30
velocity.y += gravity * delta
velocity.x = direction.x
velocity.z = direction.z
velocity = move_and_slide(velocity, Vector3(0, 1, 0))
if is_on_floor() and Input.is_key_pressed(KEY_SPACE):
velocity.y = 10
var speed = 500
var direction = Vector3()
var gravity = -9.8
var velocity = Vector3()
var sens = 0.2
onready var cam = get_node("Camera")
onready var anim_player = $AnimationPlayer
onready var camera = $Camera
onready var raycast = $Camera/RayCast
func _ready():
print("hi")
func _input(event):
if event is InputEventMouseMotion:
var movement = event.relative
cam.rotation.x += -deg2rad(movement.y * sens)
cam.rotation.x = clamp(cam.rotation.x, deg2rad(-45), deg2rad(180))
rotation.y += -deg2rad(movement.x * sens)
func fire():
if Input.is_action_pressed("Fire"):
if not anim_player.is_playing():
if raycast.is_colliding():
var target = raycast.get_collider()
if target.is_in_group("enemy"):
target.health -=damage
anim_player.play("AssaultRifleFire")
else:
anim_player.stop()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
fire()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if Input.is_key_pressed(KEY_ESCAPE):
get_tree().quit()
direction = direction.normalized().rotated(-rotation.y)
direction = Vector3(0, 0, 0)
if Input.is_action_pressed("Left"):
direction.z -= 1
if Input.is_action_pressed("Right"):
direction.z += 1
if Input.is_action_pressed("Forward"):
direction.x += 1
if Input.is_action_pressed("Back"):
direction.x -= 1
direction = direction.normalized()
direction = direction * speed * delta
if velocity.y > 0:
gravity = -20
else:
gravity = -30
velocity.y += gravity * delta
velocity.x = direction.x
velocity.z = direction.z
velocity = move_and_slide(velocity, Vector3(0, 1, 0))
if is_on_floor() and Input.is_key_pressed(KEY_SPACE):
velocity.y = 10发布于 2021-09-08 12:04:16
旋转后的函数需要两个参数,请参阅gdscript手册:
轴旋转(轴: Vector3,phi:
Vector3 )
将此向量绕给定轴旋转φ弧度。轴必须是规范化的向量。
因此,像这样的东西
direction = direction.normalized().rotated(Vector3(0,-1,0),rotation.y)
https://stackoverflow.com/questions/64836382
复制相似问题