Search Unity

C# making a VIEWFINDER that points to one of many fires, Vector3.RotateTowards

Discussion in 'Scripting' started by jbarbeau, Aug 15, 2017.

  1. jbarbeau

    jbarbeau

    Joined:
    Dec 21, 2012
    Posts:
    46
    I'd appreciate some help. I'm having difficulty with RotateTowards, the example in the documentation SEEMED to APPLY, but I can't get it to work.

    I have a Flight Sim that trains firefighters. In the plane's instrument panel I need to make a View Finder or a TCAS. There are multiple fires in the zone, and a controller assigns which fire each pilot is to attack. The Viewfinder guides him/her to the fire using the vector from the plane to the fire rotated according to the planes.forward direction.

    Pic 1 shows that the CONTROLLER has set 3 fires, and the plane (a 747) is in the middle. Fire 1 should be to the pilot's left, 2 straight ahead (North) and 3 east or to the right.

    Pics 2 and 3 show the DEBUG.DRAWLINE() that points to each fire when selected as the target, THIS WORKS.

    Pics 4 and 5 show the TCAS instrument display (green circle) It should have a line showing the RELATIVE Direction to the assigned fire, but it's not right. I show the TCAS assigned to Fire 2 and I've turned the plane in the editor by hand so I can simulate it's orientation in flight. The direction is wrong, you can probably see, Here's the code:

    Vector3 firepos = (taken transform.position from a list of fires) ;
    Vector3 planepos = PlaneTransform.position;

    ToFire = firepos - planepos; // calculate heading target - plane position

    float speed = 2;
    float step = speed * Time.deltaTime; // from UNity example
    var planeforward = PlaneTransform.forward;
    newDir = Vector3.RotateTowards( planeforward* ToFire.magnitude, ToFire, step, 100.0f);

    // DEBUG.DRAWRAY DRAWS IN THE RIGHT PLACE AS SHOWN IN SCREENSHOT
    Debug.DrawRay( firepos, planepos, Color.red, 0f, false); // 4th param is time the Ray is shown, 0 means 1 frame, 4th param is "Should the line be obscured by other objects closer to the camera?" - NO

    // SECOND HALF---------------------
    // ORIENT THE VECTOR TO 90 OF THE INSTRUMENT (CAN- DO THIS DIFFERENTLY TAKING BY // SIMPLY TRANSLATING X AND Z TO X AND Y OF THE INSTRUMENT) THEN TURN THE VECTOR BY // THE PLANE.FORWARD DIRECTION correctly turn it in opposite until when facing the fire, the line is /straight up on the display.... this means flying to the fire

    topdown = Quaternion.Euler(90, 0, 0) * newDir; // rotate XZ plane around X to now XY plane
    topdown *= .0006f; // ScaleTCAS; // scale it
    topdown += TCAStransform.position; // center of TCAS background
    topdown.z += 0.5f; //MAKE LINE in front of background

    Vector3 center = new Vector3(TCAStransform.position.x, TCAStransform.position.y, TCAStransform.position.z);
    center.z += 0.5f; // MAKE CENTER in front of instrument background

    DrawLineJTB(center, topdown, Color.red, 0.001f);

    void DrawLineJTB(Vector3 start, Vector3 end, Color color, float duration) // USING LineRenderer
    {
    if (end != LastEndPoint)
    {
    LastEndPoint = end;
    if (TCASLine) Destroy(TCASLine);

    TCASLine = new GameObject();
    TCASLine.transform.position = start;
    TCASLine.AddComponent<LineRenderer>();
    LineRenderer lr = TCASLine.GetComponent<LineRenderer>();
    lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
    lr.SetColors(color, color);
    lr.widthMultiplier = 0.05f;
    lr.SetPosition(0, start);
    lr.SetPosition(1, end);
    }
    }


    I ALSO tried to do this with Transform.TransformPoint and Transform.TransformDirection.....

    This may be part of my confusion, Vector3.RotateTowards() gives me a DIRECTION, after scaling, etc. I use it to pass the ENDPOINT of the DrawLine function. Is this right? If not what should I be doing?

    Thanks for your input.
     

    Attached Files: