【问题标题】:Godot player not walking in the directionGodot播放器没有朝这个方向走
【发布时间】:2021-07-27 05:56:07
【问题描述】:

所以我是 godot 的新手,我遇到了问题。当相机移动时,玩家的身体不会跟随相机当我用相机环顾四周时,玩家的身体保持不变,例如如果我向右转时使用wasd,那将是aswd。球员运动很艰难,但我得到了帮助。如果您能提供帮助,那将非常酷,我只是想学习编码。这对我来说很难。

p.s 抱歉语法不好

extends KinematicBody

signal hit

# 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 = 50
# Vertical impulse applied to the character upon jumping in meters per second.
export var jump_impulse = 30
# Vertical impulse applied to the character upon bouncing over a mob in meters per second.
export var bounce_impulse = 16

# stats
var curHP : int = 10
var maxHP : int = 10
var ammo : int = 15
var score : int = 0


# cam look
var minLookAngle : float = -90.0
var maxLookAngle : float = 90.0
var lookSensitivity : float = 10.0

# vectors
var vel : Vector3 = Vector3()
var mouseDelta : Vector2 = Vector2()

# components
onready var camera : Camera = get_node("Camera")
onready var muzzle : Spatial = get_node("Camera/Muzzle")


# Emitted when a mob hit the player.


var velocity = Vector3.ZERO


func _physics_process(delta):
var direction = Vector3.ZERO

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_back"):
    direction.z += 1
if Input.is_action_pressed("move_forward"):
    direction.z -= 1
    
#sprinting
if Input.is_action_pressed("move_sprint"):
    speed = 50
if Input.is_action_just_released("move_sprint"):
    speed = 14



velocity.x = direction.x * speed
velocity.z = direction.z * speed

# Jumping.
if is_on_floor() and Input.is_action_just_pressed("move_jump"):
    velocity.y += jump_impulse

velocity.y -= fall_acceleration * delta
velocity = move_and_slide(velocity, Vector3.UP)



func _ready():

# hide and lock the mouse cursor
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 


    
func _process(delta):

# rotate the camera along the x axis
camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta

# clamp camera x rotation axis
camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)

# rotate the player along their y-axis
rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta

# reset the mouseDelta vector
mouseDelta = Vector2()

func _input(event):

if event is InputEventMouseMotion:
    mouseDelta = event.relative

【问题讨论】:

    标签: godot


    【解决方案1】:

    如果您希望移动基于相机的方向...

    然后根据移动:

    var direction = Vector3.ZERO
    
    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_back"):
        direction.z += 1
    if Input.is_action_pressed("move_forward"):
        direction.z -= 1
    

    关于camera的方向。

    我们将使用camera.global_transform.basistransformbasis 为我们提供了一组与变换空间的轴对齐的向量。

    使用camera.transform 代替camera.global_transform 将不起作用,因为CameraKinematicBody 的子代。

    那么你的代码最终是这样的:

    var direction = Vector3.ZERO
    var camera_x = camera.global_transform.basis.x
    var camera_z = camera.global_transform.basis.z
    
    if Input.is_action_pressed("move_right"):
        direction += camera_x
    if Input.is_action_pressed("move_left"):
        direction -= camera_x
    if Input.is_action_pressed("move_back"):
        direction += camera_z
    if Input.is_action_pressed("move_forward"):
        direction -= camera_z
    

    OP 请求的完整脚本:

    extends KinematicBody
    
    #signal hit
    
    # 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 = 50
    # Vertical impulse applied to the character upon jumping in meters per second.
    export var jump_impulse = 30
    # Vertical impulse applied to the character upon bouncing over a mob in meters per second.
    # export var bounce_impulse = 16
    
    # stats
    # var curHP : int = 10
    # var maxHP : int = 10
    # var ammo : int = 15
    # var score : int = 0
    
    
    # cam look
    var minLookAngle : float = -90.0
    var maxLookAngle : float = 90.0
    var lookSensitivity : float = 10.0
    
    # vectors
    # var vel : Vector3 = Vector3()
    var mouseDelta : Vector2 = Vector2()
    
    # components
    onready var camera : Camera = get_node("Camera")
    # onready var muzzle : Spatial = get_node("Camera/Muzzle")
    
    
    # Emitted when a mob hit the player.
    
    
    var velocity = Vector3.ZERO
    
    
    func _physics_process(delta):
        var direction = Vector3.ZERO
        var camera_x = camera.global_transform.basis.x
        var camera_z = camera.global_transform.basis.z
    
        if Input.is_action_pressed("move_right"):
            direction += camera_x
        if Input.is_action_pressed("move_left"):
            direction -= camera_x
        if Input.is_action_pressed("move_back"):
            direction += camera_z
        if Input.is_action_pressed("move_forward"):
            direction -= camera_z
            
        #sprinting
        if Input.is_action_pressed("move_sprint"):
            speed = 50
        if Input.is_action_just_released("move_sprint"):
            speed = 14
    
    
    
        velocity.x = direction.x * speed
        velocity.z = direction.z * speed
    
        # Jumping.
        if is_on_floor() and Input.is_action_just_pressed("move_jump"):
            velocity.y += jump_impulse
    
        velocity.y -= fall_acceleration * delta
        velocity = move_and_slide(velocity, Vector3.UP)
    
    
    
    func _ready():
    
        # hide and lock the mouse cursor
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 
    
    
        
    func _process(delta):
    
        # rotate the camera along the x axis
        camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta
    
        # clamp camera x rotation axis
        camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)
    
        # rotate the player along their y-axis
        rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta
    
        # reset the mouseDelta vector
        mouseDelta = Vector2()
    
    func _input(event):
    
        if event is InputEventMouseMotion:
            mouseDelta = event.relative
    

    【讨论】:

    • 我尝试添加它但它给了我错误你能把它放在一起吗
    • @johnlohmann 它对我有用,你的代码,只是替换了那部分代码。我将Camera 作为KinematicBody 的子代,并添加了您的代码使用的输入映射。我不得不修复一些缩进(编辑:这是错误吗?)。我还删除了部分未使用的代码,因为它们给了我警告(编辑:这不是必需的)。你遇到了什么错误?
    • 错误如果出现意外令牌:p.s.谢谢你帮助我
    • @johnlohmann 这在我身上发生了一些复制的代码。应该是新线吧。在出现错误的地方,尝试删除新行(所以两行是单行)并再次插入新行。
    • 很抱歉给你按摩,但还是不行,你能把它放在一起吗?如果不是那没关系
    猜你喜欢
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多