Example #1
0
    //----------------------------------------------------------------------------------------------------------------
    void Update()
    {
        if (trackobject == null || !TrackTarget.Equals(trackobject.name))
        {
            trackobject = GameObject.Find(TrackTarget);                 // child-init
        }
//		Debug.Log("Doppel me: "+transform.name+", TrackTarget: " + TrackTarget + ", ctclient.custom: " + ctclient.custom+", trackobject: "+trackobject);

        if (ctunity.isReplayMode())
        {
            transform.localScale = myScale;                                         // appear, let ctclient drive
        }
        else
        {
            transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);                // hide
            if (trackobject != null /* && ctclient.isLocalObject() */)              // follow if local target
            {
                transform.position = trackobject.transform.position;
                transform.rotation = trackobject.transform.rotation;
            }
        }
    }
Example #2
0
    //----------------------------------------------------------------------------------------------------------------

    /*
     * Camera logic on LateUpdate to only update after all character movement logic has been handled.
     */
    void LateUpdate()               // LateUpdate reduces playback jitter
    {
        if (rightMouseDown)         // right-mouse drag cammera rotation
        {
            // deltaPos is normalized mouse-motion relative to mouse-down
            xDeg = deltaPos.y * 360f;                           // scale such that 1x screen.width = full-circle orbit
            yDeg = deltaPos.x * 360f;
            // set camera rotation
            cameraRotation = Quaternion.Euler(new Vector3(xDeg, yDeg, 0) + startCameraRotation);
        }
        else
        {
            startRotation = transform.rotation;
        }

        Vector3 tposition = oldTarget;

        if (target == null)
        {
            GameObject gotarget = GameObject.Find(targetName);                    // try to re-target
            if (gotarget != null)
            {
                //			target = gotarget.transform;
                //			tposition = target.position;
            }
        }
        else
        {
            if (Vector3.Magnitude(target.position - oldTarget) > 0.01f)
            {
                newTarget = false;                                                                  // no smoothing if moving target
            }
            tposition = target.position;
            //           Debug.Log("target.postion: " + target.position + ", oldTarget: " + oldTarget + ", newTarget: " + newTarget);
        }
        oldTarget = tposition;

        ////////Zoom Position
        // affect the desired Zoom distance with mouse scrollwheel
        float dd = Input.GetAxis("Mouse ScrollWheel");

        desiredDistance -= dd * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        //clamp the zoom min/max
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        // For smoothing of the zoom, lerp distance
        currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);

        // special close-up logic:  for "vehicles" with pushForward, lock camera rotation to object-direction
        Vector3 snapOffset = Vector3.zero;
        float   snapCheck  = snapOn ? snapOut : snapIn;

        if (rightMouseDown)
        {
            newTarget = false;
            if (deltaPos.magnitude > 0F)
            {
                Vector3 desiredRotationDeg = transform.eulerAngles + new Vector3(xDeg, yDeg, 0F);
                float   clampX             = desiredRotationDeg.x;
                float   tymin = 360F - yLimit;
                if (clampX > 0 && clampX < 180 && clampX > yLimit)
                {
                    desiredRotationDeg = new Vector3(yLimit, transform.eulerAngles.y, 0F);
                }
                else if (clampX > 180 && clampX < tymin)
                {
                    desiredRotationDeg = new Vector3(tymin, transform.eulerAngles.y, 0F);
                }

                transform.eulerAngles = desiredRotationDeg;
                startPos = Input.mousePosition;  // reset (incremental rotations more reliable?)
            }
        }
        else if ((currentDistance < snapCheck) && isTargetVehicle())
        {
            snapOn          = true;
            extraOffset     = (snapCheck - currentDistance) / snapCheck;
            desiredRotation = Quaternion.Euler(new Vector3(0, target.transform.rotation.eulerAngles.y, 0)) * cameraRotation;

            // Lerp jiggles with PlayerController Lerp.  But instant-rot doesn't ease-in distant/closeup transition
            // Lerp if large delta, jump if small
            float deltaRot = Math.Abs((desiredRotation.eulerAngles.y - transform.rotation.eulerAngles.y));
            while (deltaRot > 180)
            {
                deltaRot -= 360F;
            }

            if (Math.Abs(deltaRot) < 30F || ctunity.isReplayMode())
            {
                transform.rotation = desiredRotation;                  // actually smoother to have camera lock-on rotation (let playerController dampen motion)
            }
            else
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, Time.deltaTime * zoomDampening);              // fast!?
            }
        }

        else
        {
            cameraRotation = Quaternion.Euler(new Vector3(30F, 0, 0));                  // default closeup start angle
            extraOffset    = 0;
            snapOn         = false;
        }

        // calculate position based on the new currentDistance
        Vector3 tpos = tposition - (transform.rotation * Vector3.forward * currentDistance) + targetOffset;

        tpos = new Vector3(tpos.x, tpos.y + extraOffset, tpos.z);
        float dx = Vector3.Magnitude(tpos - transform.position);

        if (newTarget)
        {
            //transform.position = Vector3.SmoothDamp(targetPos1, targetPos2, ref velocity, 1f / zoomDampening);
            transform.position = Vector3.Lerp(transform.position, tpos, Time.deltaTime * zoomDampening);
//            Debug.Log("Lerp target dx: "+dx);
            if (dx < 0.01F)
            {
                newTarget = false;
            }
        }
        else
        {
            //         velocity = Vector3.zero;
//            Debug.Log("jump target");
            transform.position = tpos;
        }
    }