This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Ive been trying to create a "spring" function that does a springy tween from one vector to another. The function ive tried to make, does absolutely nothing. Im very new to creating custom functions so the problem could be simple, or could be completely out of the ball park.

code:

func _process(delta):
    spring(parent.rotation,camera.global_transform.basis.get_euler(),0.5,0.1)


func spring(f:Vector3,t:Vector3,s:int,w:int):
    var sprspd:Vector3
    sprspd = lerp(sprspd,(t-f)*s,w)
    f+=sprspd
Godot version 3.3
in Engine by (70 points)

1 Answer

+1 vote
Best answer

"In GDScript, only base types (int, float, string and the vector types) are passed by value to functions (value is copied). Everything else (instances, arrays, dictionaries, etc) is passed as reference."

So in your case parent.rotation is passed by value and does not affect the parents rotation. Maybe you should pass just parent and use f.rotation inside the function instead of just f.

OR just return the value f and set parent.rotation in the calling function. E.g.

func _process(delta):
    parent.rotation =spring(parent.rotation,camera.global_transform.basis.get_euler(),0.5,0.1)


func spring(f:Vector3,t:Vector3,s:int,w:int):
    var sprspd:Vector3
    sprspd = lerp(sprspd,(t-f)*s,w)
    return f + sprspd
by (810 points)
selected by

I cant seem to understand why its not working

This works:

    extends Spatial


var sprspd  = Vector3()



func _process(delta):
    var from = global_transform.origin
    var to = Vector3(0.0,0.0,0.0)
    var spring = 0.5
    var weight = 0.1

    sprspd = lerp(sprspd,(to-from)*spring,weight)
    from+=sprspd

    global_transform.origin=from

But doing it this way, i have to create a unique sprspd variable for each thing i want to tween. I cant figure out how to turn that into a custom function for easy reuse.

You need to re-read my post above. Did you try it?

tried it your way, it did the same thing as mine did, nothing was moving.

ok this works. I'll let you figure out the what your problem is :) In all honesty this is a Godot editor problem for not catching it.

extends Spatial

var sprspd  = Vector3()
func _process(delta):

    var from = global_transform.origin
    var to = Vector3(0.0,0.0,0.0)
    var spring = 0.5
    var weight = 0.1    
    global_transform.origin=spring(from,to,spring,weight)

func spring(f:Vector3,t:Vector3,s:float,w:float):
    sprspd = lerp(sprspd,(t-f)*s,w)
    return f + sprspd

Alright, that definitely works, big thanks for your help. there's only one last minor issue of having to add a unique "sprspd" variable at the top of the script for every tween, any thought or ideas on how to tackle this?

Depends how many tweens you planning to store and how you are going to use them. An array is probably the way you want to go.

Thanks for all your help, i really apreciate it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.