I am trying to have an object (kinematic projectile) follow a specific path. So for example, perhaps its path could be represented by the line y=3x or y=2x^2.
In the linear example, I can get the projectile to fire in correct direction but its speed changes depending on the slope of the line. I am sure there is a way that I can normalize the vector (I think normalization is what I want to do). In looking at the Unity documentation I saw that there was a Vector3.Normalize() but no example on how to implement it and having tried a bunch of times I am obviously doing something wrong.
I am still very new to Unity so my apologies for what is probably a very simple question.
It is possible to use a normalised vector to represent a straight line, but it will be difficult to apply the same idea to quadratic curves, etc. If there is only a specific set of curves you need to use, you may be able to vary the speed of the projectile’s motion according to the rate of change of the function. For example, you will typically plot the Y position for a set of X values by incrementing the X value regularly:-
var increment = 0.1;
for (x = 0.0; x < 10.0; x += increment) {
// Plot position.
}
If you know the rate of change for your function, you can vary the increment value so that the change in Y is constant. However, you will generally need to resort to some differentiation to do this.
The idea of using the derivative to plot the function makes a lot of sense. Follow up question. I confused between the difference between these two ways to represent a vector: So assuming I want to move an object to the right:
I would think according to the documentation, that these two things are equivalent since Vector3.right is the supposed to be the same thing as Vector3(1,0,0). The second line does not compile. So my question is, what is the difference between these two vectors?[/code]
Works great now that I added “new”. So basically, if I am constructing a vector such as Vector3(1,0,0) I have to use the word new but if I am using a Vector of the type Vector3.right Unity already has that type in memory so I do not have to declare it? Hopefully I am making sense.
Yes. In C#, you need to new up your own Vector3’s, but you don’t have to worry about it for the built-in Vector definitions like .right, .left, .identity etc.
This thread does a bonzer job of explaining what is going on with the Vector3 struct, and what the deal is with the variations in the JS and C# implementations in Unity: