Topic was automatically imported from the old Question2Answer platform.
Asked By
Penrose
So I am trying to just rotate the camera/player 90 degrees (dungeon crawler style). I got it working but it would only turn once and get stuck in a loop. I found the -0 in the transform and have tried a few things but no luck
extends KinematicBody
var rotation_speed = .50
var targettransform
var rotation_lerp = 0
func _ready():
targettransform = self.transform.basis
func _process(delta):
if self.transform.basis != targettransform:
rotate_player(delta)
#not transform.basis.is_equal_approx(targettransform, 0.0001):
if self.transform.basis == targettransform:
rotation_lerp = 0
get_input()
print(transform.basis)
print(targettransform)
func rotate_player(delta):
if rotation_lerp < 1:
rotation_lerp += delta * rotation_speed
elif rotation_lerp > 1:
rotation_lerp = 1
transform.basis = transform.basis.slerp(targettransform , rotation_lerp).orthonormalized()
func get_input():
if Input.is_action_just_pressed("turn_left"):
print("left")
targettransform = self.transform.basis.rotated(Vector3(0,1,0),1.5707963).orthonormalized()
print (self.transform.basis.rotated(Vector3(0,1,0),1.5708))
if Input.is_action_just_pressed("turn_right"):
print("right")
targettransform = self.transform.basis.rotated(Vector3(0,1,0),-1.5707963).orthonormalized()
print (self.transform.basis.rotated(Vector3(0,1,0),-1.5708))
if Input.is_action_just_pressed("Test_button"):
print (transform.basis)
print (targettransform)
Then I tried 0.0001 to 1 for the is_equal_approx
func _process(delta):
if not self.transform.basis.is_equal_approx(targettransform, 1):
rotate_player(delta)
if transform.basis.is_equal_approx(targettransform, 1):
rotation_lerp = 0
get_input()
Animations like that can be done using a Tween. Note that you may need to cancel and restart your Tween if the player turns again before the animation is done.
Since I couldn’t out the way to do it with animation tween so I rotated the whole transform (instead of basis) and reset the origin so it did spin around world’s origin.
extends KinematicBody
var rotation_speed = 7
var target_transform
var rotation_lerp = 1
var party_state
var FowardVector = 0
var old_location
func _ready():
target_transform = self.transform
old_location = self.transform
party_state == "ready"
func _process(delta):
if party_state == "ready":
get_input()
if not transform.is_equal_approx(target_transform):
if party_state == "turning":
rotate_party(delta)
if party_state == "moving":
pass
if transform.is_equal_approx(target_transform):
rotation_lerp = 1
old_location = self.transform
party_state = "ready"
func rotate_party(delta):
if rotation_lerp < 75:
rotation_lerp += delta * rotation_speed * rotation_lerp
elif rotation_lerp > 75:
rotation_lerp = 75
self.transform = self.transform.interpolate_with(target_transform, rotation_lerp * delta)
self.transform.origin = old_location.origin
func move_party():
pass
# #velocity = -transform.basis.z * speed
# if transform.origin.distance_to(target) < .5:
# target = null
# velocity = Vector3.ZERO
func get_input():
if Input.is_action_just_pressed("turn_left"):
print("left")
target_transform = target_transform.rotated(Vector3(0,1,0),1.5707963).orthonormalized()
target_transform.origin = old_location.origin
party_state = "turning"
if Input.is_action_just_pressed("turn_right"):
print("right")
target_transform = target_transform.rotated(Vector3(0,1,0),-1.5707963).orthonormalized()
target_transform.origin = old_location.origin
party_state = "turning"