How do you tween "object_rotate_local" on Vector.UP ??

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By eyeEmotion

Ok, I have been at this for a while, most didn’t work, some work but then continu on rotating and I don’t know a what to adress to stop that roation at the angle I want.

But since the “button” that invokes this camera angle change is a joystick which can be held or let go at any moment and the angle needs go from one to the other smoothly… and back when let go.
I think it’s best to use a tween and interpolate. And apparantly you have interpolate_method and interpolate_property. Most of the parameters I understand, but the 2nd one is the problem, what you need to fill in as property or method. Because online I see it different with everybody and none of them work. It’s annoying to constantly hitting these sort of problems without finding/getting a straigth answer. It all goes from very easy to extreme hard with no explanation at all. You find posts with problems, but most of the time only mentioned they managed to get it working, but not sharing what that exactly.

So for now, I need help getting my tween right. What do I need to do to rotate around my Y-axis of the camera. I know it can work, I’ve done it with object_local_rotate and having a lerp in it, rotate_object_local(Vector3.UP, lerp(0, 2, look_around * t)) but that just keeps on spinning and I don’t which value to stop it.

So this is the code I’m trying to work with. This function gets called in the _physics_process():

var tween = Tween

func get_input(deltaVal):
    if (!keyb):
	look_around = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
	if (abs(look_around) == 1.0):
		print("tweening")   #just to test if it's even getting here.
		tween.interpolate_method(self, "rotate_object_local", 0, 20 * look_around, 3, Tween.TRANS_LINEAR, Tween.EASE_OUT)

I don’t want a TweenNode in my Scene-Tree, want to do this by code.
So how get I get things turning? For the 2nd parameter, I’ve set others things aswell, like set_rotation and such.
I’ve set 0 and 20 for the next parameters, because I want to start from the rotation I’m currently at, to 20 degrees either left or right ( look_around is either going to be -1 or 1 so it know which way to turn).
I know this probably could be wrong, because I’ve done a print("looking around: %s" % str(rad2deg(get_camera_transform().basis.get_euler().y))) and it said something of -179,998887.
The 3 is hopefully the seconds?

And while I’m at it… didn’t get a timer to work either. Apparently that is what I need, because I only want the tween to happen when the joystick is held at 1 or -1 for more than two seconds.

Don’t worry about the if (abs(look_around) == 1.0): . It works and I’ll be changing it later as somebody gave me the advice of trying to avoid using == and it’s better to do <or >.

And I assume, if the true statement works, the false statement will automatically work by changing parameter 3 and 4, right?

I understand what Tweening is, I’ve done it many times in Flash/ActionScript (yeah I know, it isn’t used anymore).

So I just want help to get this working. What does it take to make this tween work?

Atleast got the tween moving.

tween.interpolate_method(self, "rotate_y", deg2rad(0.0), deg2rad(20.0) * look_around, 3, Tween.TRANS_LINEAR, Tween.EASE_OUT)

I rotates now, but too much… and seems to be dependant on how hard I pull the joystick. If I do it quick, it spins several times. If I just lightly tap it, it goes real slow.

eyeEmotion | 2020-04-30 23:12

Ok, almost there… but getting some jitter if I’m holding the joystick. I think because it keeps executing the tween over and over, because I keep holding the joystick.
Maybe add a boolean? No, not helping…

Camera Steering Jitter holding joystick

func get_input(deltaVal):
if (!keyb):
	look_around = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
	if (abs(look_around) > 0.99):
		tween.interpolate_method(self, "rotate_y", deg2rad(0.0), deg2rad(1.2) * look_around, 0.3, Tween.TRANS_LINEAR, Tween.EASE_OUT)

And still needing to implement the timer somehow.

eyeEmotion | 2020-04-30 23:26

:bust_in_silhouette: Reply From: davidoc

A tween is not the best option for this, you were right using a lerp function, what you need is a variable to hold the current rotation and interpolate it to the new velocity (0 or your angle), look at this code:

extends Camera
var hold_time = 0.0
var rotate_velocity = 0.1
var rot_velocity = 0.0

func _physics_process(delta):

func rotate_me(deltaVal):
	var read_input = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
	var look_around = 0.0
	if(read_input != 0):
		hold_time += deltaVal
		hold_time = 0.0
	if(hold_time > 2.0):
		look_around = rotate_velocity * read_input
	rot_velocity = lerp(rot_velocity, look_around, deltaVal)
	rotate_object_local(Vector3.UP, rot_velocity)

hold_time will help you to start the rotation after n seconds.
rotate_velocity is the radians per second you want the camera to rotate
rot_velocity holds the current rotation velocity (we interpolate it to reach the desired value).
Of course you may want to adjust the code to match what you want to achieve


thanks for the reply, because I’m going all directions with this . So no tweening then? Just when I was able to get it to work . On the upside, all these mistakes I’m making, is every step closer to understanding how to approach things and how Godot/game-development works .

You say I may want to adjust to code to match what I want to achieve. By that you mean the values?
With the

For rotations, does it always need to be compared to radians? Was my deg2rad() used correctly? Because I find it easier to think in degrees, that’s something I can visualize for myself.

I’m going to try to implement your code now and see what I can make of it . Who knew setting up an “interactive” camera would be so much trouble. Will be happy when the day comes that I can do this blindly.

eyeEmotion | 2020-05-01 14:29

Ok, so that code works flawlessly. Thanks! Tweeking it a bit because I’m noticing 2 seconds is too long.
Now I just need to compare it to the things I tried and see where I was doing right and where I was doing wrong.

So I can follow the code and what you did, but what I still don’t get is, how does it know when to stop rotating? rotate_velocityis each radian (or in my head comparison “degree”) per deltaTime hold_time . So each frame.
But where is the “hold up!!, you’ve gone far enough now” code? Because apparantly that is what I keep doing wrong, wanting to make if-statements or for-loops and thus to comparisons to values.
rot_velocity = lerp(rot_velocity, look_around, deltaVal) This is the bit that is throwing me of. Because you would normally change the wheight value, right? And now your actually changing the 2nd parameter of lerp()?

It seems as if my programming skills in webdesign/web-development are hindering my progress instead of speeding it up. I have to unlearn stuff to then learn new stuff.


eyeEmotion | 2020-05-01 14:48

I changed the lerp parameter to interpolate from the current rotation velocity (rot_velocity) to the desired velocity(look_around), this way you go smoothly from zero to rotate_velocity and from rotate_velocity to zero.

When to stop depends in the conditions you need to define (just rotate certain angles, when you find a target, a fixed time), you can add the conditions in the read_input part: if you have rotated too much to he left don’t take the left input(or multiply it by 0).

davidoc | 2020-05-01 16:39