【问题标题】:cant add camera rotation in gdscript无法在 gdscript 中添加相机旋转
【发布时间】:2021-09-08 20:04:15
【问题描述】:

我正在尝试在 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

【问题讨论】:

    标签: gdscript


    【解决方案1】:

    旋转函数需要两个参数,请参阅 gdscript 手册:

    Vector3 旋转(轴:Vector3,phi:float)

    将此向量围绕给定轴旋转 phi 弧度。轴必须是归一化向量。

    因此是这样的

    direction = direction.normalized().rotated(Vector3(0,-1,0),rotation.y)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多