0 votes

I'm trying to have the player move towards an object in the game, and am using this piece of code to do it:

motion.x = (self.position.x - $Chain/Tip.position.x).normalized() * speed * chain_velocity

However, when I try to run it, it gives me this error:

Invalid call. Nonexistent function 'normalized' in base 'float'.

Any idea why, or how I can fix it?

Godot version 3.2.3
in Engine by (105 points)

2 Answers

0 votes
Best answer

normalized() is a Vector math operation (only for complete vectors), but what it does is actually a very simple calculation: vector / vector.length(), so maybe try:

motion.x = (self.position.x - $Chain/Tip.position.x) / motion.length() * speed * chain_velocity

Also to answer the question title, converting Floats to Vectors is nice and straight-forward, you can simply go: var vector = Vector2(vector.x, vector.y)

So another way you might be able to solve your problem is to write:

motion = Vector2((self.position.x - $Chain/Tip.position.x) * speed * chain_velocity, motion.y).normalized()

Hope it helps!

by (1,008 points)
selected by

Alright— yeah! That's super awesome!

I've also just found a way to fix the AnimationPlayer issue that only requires minor code tweaking, then won't have to make two sets of animations for left and right from here on!

That would be great!

I'm working on an answer for that question now.

With this question/answer, I'm just going to leave my answer as is since what we were discussing ended up being pretty different to the OP, and both me and Imaginaryzero covered that pretty well.

Once again though, what you're making looks really fun so far. And your code looks great! Honestly. But you just keep setting up mystery traprooms for yourself ● ‿↼

Thanks :) lol

Hi, sorry it some time but I have completed the other answer.
I just wanted to make sure that the solution would definitely work in your project (it did!). Feel free to comment here or there if you need more details regarding its implementation — though the answer covers everything I think you need.

0 votes

(Assuming you're using 2D) Vector2 is simply two floats.
motion.x and motion.y are floats, and motion is a Vector2. The same is true for Vector3.
You would have to normalize motion itself.
This might solve the error:

 motion.x = ((self.position.x - $Chain/Tip.position.x) * speed * chain_velocity)
 motion = motion.normalized()
by (14 points)

Received this error: Invalid set index 'x' (on base: 'Vector2') with value of type 'Vector2'.

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.