Hi, I am trying to flip my character, but I cannot get it to work, it’s printing the numbers correctly, but I cannot visually see the character changing orientation.
Code
extends CharacterBody3D
@export var speed = 7
@export var jumpPower = 15
@onready var camera_controller: Node3D = $Camera_controller
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
scale.x = 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
if(!is_on_floor()):
velocity += get_gravity()
elif(Input.is_action_just_pressed("jump")):
velocity.y = jumpPower
var dir = Input.get_axis('left' , 'right')
if(dir):
velocity.x = dir * speed
else:
velocity.x = move_toward(velocity.x , 0 , speed)
move_and_slide()
camera_controller.position = lerp(camera_controller.position , position , 0.1)
if Input.is_action_pressed('right'):
scale.x = 1
elif Input.is_action_pressed("left"):
scale.x = -1
print(scale.x)
I have no clue why it is not working and help is greatly appreciated.
I have made some advancments in my code, it flips, but it flips weirdly. Like in this video:
I want the player to flip along the X axis when the different buttons are being pressed. I there’s a better more efficient way, I would gladly hear about it. I do not know if I answered your question correctly though
Code
extends CharacterBody3D
@export var speed = 7
@export var jumpPower = 15
@onready var camera_controller: Node3D = $Camera_controller
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
scale.x = 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
if(!is_on_floor()):
velocity += get_gravity()
elif(Input.is_action_just_pressed("jump")):
velocity.y = jumpPower
var dir = Input.get_axis('left' , 'right')
if(dir):
velocity.x = dir * speed
else:
velocity.x = move_toward(velocity.x , 0 , speed)
move_and_slide()
camera_controller.position = lerp(camera_controller.position , position , 0.1)
if Input.is_action_just_pressed("right"):
scale = Vector3(1,1,1)
elif Input.is_action_just_pressed("left"):
scale = Vector3(-1,1,1)
print(scale.x)
Edit:
When I meant “Different buttons are being pressed” I meant the left and right buttons.
Ok… I finally got it working. I had to rather instead of shifting the s
scale, I rather shifted the rotation. My number is EXTREMLY specific, coming from the print result in the console. So other peoples directions will vary. This is how the code looks.
Code
extends CharacterBody3D
@export var speed = 7
@export var jumpPower = 15
@onready var camera_controller: Node3D = $Camera_controller
var LeftOrRight = null
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
scale.x = 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
if(!is_on_floor()):
velocity += get_gravity()
elif(Input.is_action_just_pressed("jump")):
velocity.y = jumpPower
var dir = Input.get_axis('left' , 'right')
if(dir):
velocity.x = dir * speed
else:
velocity.x = move_toward(velocity.x , 0 , speed)
move_and_slide()
camera_controller.position = lerp(camera_controller.position , position , 0.1)
if Input.is_action_just_pressed("right"): #Checks if right, if so, set LeftOrRight to "right"
LeftOrRight = "right"
elif Input.is_action_just_pressed("left"): # Checks if left, if so, set LeftOrRight to "left"
LeftOrRight = "left"
#Now we check what direction we are
if(LeftOrRight == "right"): #If 'LeftOrRight' is equal to "right" then execute the code
rotation.y = -1.570796
elif(LeftOrRight == "left"): #If 'LeftOrRight' is equal to "left" then execute this block of code
rotation.y = 1.570796
print(rotation)
This number is -PI/2, try to use this constant instead. Or deg_to_rad(-90)
Scale was probably the wrong move in the long run. Physics bodies do not like non-uniform scaling, maybe the CharacterBody3D would reset the scale silently.