- Home /

# Problem Solved [JS]

I'm trying to do some simple math, however I do not know the function in Unity, if there is a function. Lengthdir_x and Lengthdir_y were two functions in Game-Maker, which are explained if you click the link. They find the difference to the end of the vector from the origin after giving a set length and direction. How could I recreate this?

Sorry, but the function you had in your original question **does exactly the same** as the methods you've linked. The way you used them just doesn't make any sense as it results in a constant value. You did not rotate your vector since it always pointed in the same direction. Why do you actually use a vector when you want to lerp between two or more colors?

I think it would be better if you revert to your original question as this one has way less information and your original question actually contains the answer of your "new" question.

Honestly I'll just put this out here: All I was looking for all along was cosine. I thought it was the lerping that was the problem but it's toggling fine as expected. The problem were the custom functions I had put in, I had, like said, oversaw cosine and was told that using a vector and that normalizing it would work.

**Answer** by Bunny83
·
Nov 16, 2014 at 03:50 AM

The way your "lengthdir" methods work and the way you use them doesn't make much sense. The value returned will always be the passed length. Since your vector always just points in one direction, it doesn't matter what value that vector has since you normalize it. That means:

```
Vector2(blendAmount*3.12, 0) ==> Vector2(1,0)
Vector2(0, blendAmount*1.73) ==> Vector2(0,1)
```

Therefore your methods just return the passed length which is 0.5 in the first case. So you effectively pass 1.0 to your lerp method which will of course return magenta.

I'm still not sure what's you exact goal. cyan, magenta, yellow are actually the secondary colors which are usually used when using a substractive color model like CMYK. However that colors are used in printing and work way different than additive mixing which you usually use in a RGB color model

Ah, this makes sense. What the lengthdir methods are supposed to be reminiscent of Game-Maker's functions, a good idea of how they are supposed to function is here: lengthdir_x documentary (Lengthdir_y is the same but with vertical values.)

@mrpeanut188: Those methods use the passed direction vector just as pure direction. So the actual value of the x and y components are irelevant, just the ratio between them. The length of the vector is normalized (set to 1.0).

For example a vector (5,10) normalized is (0.4472, 0.8944). The two methods just take the x or y value and multiply it with the passed length. If the vector is aligned with the x axis (like you have in your first case) the y value will stay 0 and x will be 1.

It's not clear what you want to do. As i said the way you use those method doesn't make much sense and i can't figure out what's your goal here. Maybe you want to create a color based on Hue-Saturation-Brightness.

You said:

The process includes a color lerp to mix 4 colors

where have you found that information? Any source?

If Color.Lerp "Interpolates between colors a and b by t.", then by repeating the process you can mix the result of a mix. It wouldn't be even unless you balanced it, but it is a mix nonetheless. I'm going to rewrite the question since we know exactly what the problem is, I really suck at math and I'm new to Javascript, I took what was given to me in my other question, and reading the manual it seemed to make sense, guess not :/

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

"normalize" a vector but conserve the "- sign" 2 Answers

Need to normalize vectors? 2 Answers

Hexagon-Grid Distance 2 Answers

Detect if game object is near a specific tag 1 Answer

[2D] Angle to Vector conversion 1 Answer