Can't stretch "rope" properly or it just don't work

I trying to make some kind of rubber physics: object following cursor and stretching until it’s limits. The first version of the code looks like this:

extends Sprite2D

var max_strech = 3

func _physics_process(delta):
	#look_at(get_global_mouse_position())
	scale.x = get_global_mouse_position().x

and it’s work, but not how i want, because the number scale gets is too big to be near to cursor. Next version i made with guide “how to calculate distance between player and arrow”:

extends Sprite2D

var max_strech = 3
var stretch = get_global_mouse_position() - position

func _physics_process(delta):
	look_at(get_global_mouse_position())
	scale.x = stretch

and it’s just don’t work, all i get is error message: Invalid set index ‘x’ (on base: ‘Vector2’) with value of type ‘Vector2’.

Your error is telling you: “scale.x is supposed to be a single number, but you’re trying to set it to stretch, which is a 2D vector”. You probably want stretch.length(), which is the length of the vector.

But also, you’re setting stretch outside of any function, at the top level, which means it only gets set at the start of the game. You might wanna move that inside _physics_process, so it actually gets updated as the player moves the mouse around.

Thanks, now it’s work, but strange: it’s still stretching too much and when i moving cursor to sprite it’s shrinking how it should, but at one moment it’s start to stretching again.
The physics process:

func _physics_process(delta):
	var stretch = get_global_mouse_position() - position
	
	look_at(get_global_mouse_position())
	scale.x = stretch.length()/100

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.