- Home /
Calculate vector distance between 2 points on the same object
Hello everyone!
I'm taking some linear algebra courses and trying to apply some of my learning to a project I'm working on. I have a ship I'm animating. It has a particle system on either side. Currently I'm using the different axis to know when to enable each ps. So for example, if I turn too far on the X axis past 35 degrees, my ps on one side will enable. If I do the same on the Y or the Z the respective ps(s) will enable.
Trying to do this with vectors and angles in Editor mode so that I'm not limited to rotating in arbitrary angles. Currently, when I turn the ship it does sort of what I want. I'll turn the ship to the extreme left and the ps enables. But I practically have to flip the ship just to get it to enable. Trying to set it to my limit of 35 or 25 or whatever I choose.
Any tips are greatly appreciated.
constant ANGLE_LIMIT = 35;
Vector3 initialPosition = myParticles.transform.position;
Vector3 newPosition = myParticles.transform.position + mySpaceShip.transform.position;
//Get offset distance
target = newPosition - initialPos;
//vector length
distance = target.magnitude;
//normalize
distance = target/distance;
Vector3 direction = myParticles.transform.up;
angle = Mathf.Acos(Vector3.Dot(target, direction)*Mathf.Rad2deg;
if(angle > ANGLE_LIMIT )
{
myParticles.Play();
}
Ok, so walking through my code. I think I'm getting it to work. My angle always comes out to 180 by default. And then as I rotate it goes under 180 degrees. Is there a way to change that to be zero to start? That way I can say, if angle < 35 do so and so. Also, I updated my condition at the end.
if(angle < ANGLE_LIMIT)
{
myParticles.Play();
}
else
{
myparticles.Stop();
}
Answer by jgt79 · Jul 07, 2021 at 11:39 AM
Figured it out. I accidentally had a negative in front of the following line:
Vector3 direction = myParticles.transform.up;
Your answer
Follow this Question
Related Questions
'position' is not a member of 'UnityEngine.Vector3'. 1 Answer
Change a property based on angle? 1 Answer
Brand new to Unity, trying to grasp some basic stuff related to cameras and angles 1 Answer
Calculating angles to a target and turning towards it 1 Answer
Why is rotation locking to an axis? 3 Answers