Hi all,
Sounds simple enough…and it seemed to in Javascript ![]()
Now I have gotten my feet wet in unity I am migrating the tutorials + my own stuff over to c#.
My question is, given a specific point (0,0,0) I needed to get to a random position and angle away from this point. For simplicity lets say it can be any distance and any angle.
This is straight forward trigonometry, but I need to use Quaternions, and my Quaternion skills are still junior although I have read a number of tutorials.
if (Input.GetButtonDown("Fire1") target)
{
// initially for the first tests, lets just set up the camera to change
// when the player hits the space bar
Random.seed = (int)System.DateTime.Now.Ticks;
// get some random values for teh camera distance and angle
int camDist = Random.Range(20,60);
int camAngle = Random.Range(45,135);
// point on a cricle : (x,y) = (x+d(cos*angle),y+d(sin*angle))
x = camDist*Mathf.Cos(camAngle);
y = camDist*Mathf.Sin(camAngle);
// update distances
currentDistance = camDist;
camDistance = currentDistance;
y = ClampAngle(y, yMinLimit, yMaxLimit);
// Qaternion.Euler : Returns a rotation that rotates x degrees around the x axis. y degrees around the y axis. z degrees around the z axis.
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 targetPos = target.position + targetOffset;
//Quaternion direction = rotation * -Vector3.forward;
// must create a quaternion that accepts a rotation quaternion and vector 3 as arguements
Quaternion direction = Quaternion.AngleAxis(rotation, Vector3.forward);
Vector3 targetDistance = AdjustLineOfSight(targetPos, direction);
currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance, distanceVelocity, closerSnapLag * .3);
transform.rotation = rotation;
transform.position = targetPos + direction * currentDistance;
}
if you see the line "//Quaternion direction = rotation * -Vector3.forward; " - this was what I was doing in javascript, but unity has issues converting this calculation to a quaternion. I tried angle axis - but this requires a float as an argument, not an already created Quaternion.
I have done some looking into this to try and find the correct code but I guess just need a kick in the right spot. Some help would greatly appreciated.
Thanks.