2
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class CameraMove : MonoBehaviour
{
    [Header("Spin")]
    public bool spin = false;
    public Vector3 Direction;
    [Range(0, 300)]
    public float speed = 10f;
    public bool randomSpeed = false;
    public bool randomDirection = false;
    [Range(0f, 100f)]
    public float timeDirChange;
    public Vector3 defaultDirection = new Vector3(1, 0, 0);

    [Space(5)]

    [Header("Move in circles")]
    public bool moveInCircles = true;
    public GameObject rotateAroundTarget;
    public Vector3 axis;//by which axis it will rotate. x,y or z.
    public float rotationSpeed; //or the speed of rotation.
    public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
    public bool randomHeight = false;
    private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness; 


    private float nextRotationTime = 0f;
    private int counter = 0;
    private List<GameObject> Soldiers = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        Soldiers.AddRange(GameObject.FindGameObjectsWithTag("Soldier"));
    }

    private void Update()
    {
        if (randomSpeed)
        {
            speed = UnityEngine.Random.Range(0, 300);
        }

        if (spin)
        {
            if (randomDirection == false)
            {
                nextRotationTime = 0;
                timeDirChange = 0;
                Direction = defaultDirection;
            }
            else
            {
                if (Time.time > nextRotationTime)
                {
                    nextRotationTime += timeDirChange;
                    RandomDirection();
                }
            }

            transform.Rotate(Direction, speed * Time.deltaTime);
        }
        else
        {
            timeDirChange = 0;
            randomDirection = false;
            randomSpeed = false;
        }

        if (moveInCircles)
        {
            MoveInCircles();
        }
    }

    private void RandomDirection()
    {
        Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));
        while (Direction == new Vector3(0, 0, 0))
        {
            counter++;
            Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));

            if (counter == 2)
            {
                Direction = new Vector3(1, 0, 0);
                break;
            }
        }

        counter = 0;
    }

    private void MoveInCircles()
    {
        transform.RotateAround(GetAverageLocationOfSoliders(), axis, rotationSpeed);
        //transform.RotateAround(rotateAroundTarget.transform.position, axis, rotationSpeed);
        time += Time.deltaTime;
        //Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay 
        if (time > delay)
        {
            prevHeight = height;
            if (randomHeight)
            {
                height = UnityEngine.Random.Range(lowerLimit, upperLimit);
            }
            else
            {
                height = transform.position.y;
            }
            time = 0;
        }

        if (randomHeight)
        {
            //Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)  
            transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
        }
        else
        {
            transform.position = new Vector3(transform.position.x, height, transform.position.z);
        }
    }

    private Vector3 GetAverageLocationOfSoliders()
    {
        var total = new Vector3();

        foreach (var soldier in Soldiers)
            total += soldier.transform.position;

        return total / Soldiers.Count();    // Assuming Soldiers is List<Soldier>
    }
}

If it's not random height I'm doing:

height = transform.position.y;

I'm using a breakpoint on this line and see that the value of y is 1.43 but when running the game height value rest to 0.

I want that even if the user will change in real time when the game is running the camera(transform) height it will change it in real time. But I can't figure out why it's changing the height to 0.

UPDATE:

If I move the height out of the time delay check:

if (time > delay)

Then height will get the transform y value.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class CameraMove : MonoBehaviour
{
    [Header("Spin")]
    public bool spin = false;
    public Vector3 Direction;
    [Range(0, 300)]
    public float speed = 10f;
    public bool randomSpeed = false;
    public bool randomDirection = false;
    [Range(0f, 100f)]
    public float timeDirChange;
    public Vector3 defaultDirection = new Vector3(1, 0, 0);

    [Space(5)]

    [Header("Move in circles")]
    public bool moveInCircles = true;
    public GameObject rotateAroundTarget;
    public Vector3 axis;//by which axis it will rotate. x,y or z.
    public float rotationSpeed; //or the speed of rotation.
    public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
    public bool randomHeight = false;
    private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness; 

    private float nextRotationTime = 0f;
    private int counter = 0;
    private List<GameObject> Soldiers = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        Soldiers.AddRange(GameObject.FindGameObjectsWithTag("Soldier"));
    }

    private void Update()
    {
        if (randomSpeed)
        {
            speed = UnityEngine.Random.Range(0, 300);
        }

        if (spin)
        {
            if (randomDirection == false)
            {
                nextRotationTime = 0;
                timeDirChange = 0;
                Direction = defaultDirection;
            }
            else
            {
                if (Time.time > nextRotationTime)
                {
                    nextRotationTime += timeDirChange;
                    RandomDirection();
                }
            }

            transform.Rotate(Direction, speed * Time.deltaTime);
        }
        else
        {
            timeDirChange = 0;
            randomDirection = false;
            randomSpeed = false;
        }

        if (moveInCircles)
        {
            MoveInCircles();
        }
    }

    private void RandomDirection()
    {
        Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));
        while (Direction == new Vector3(0, 0, 0))
        {
            counter++;
            Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));

            if (counter == 2)
            {
                Direction = new Vector3(1, 0, 0);
                break;
            }
        }

        counter = 0;
    }

    private void MoveInCircles()
    {
        transform.RotateAround(GetAverageLocationOfSoliders(), axis, rotationSpeed);
        //transform.RotateAround(rotateAroundTarget.transform.position, axis, rotationSpeed);
        time += Time.deltaTime;
        //Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay 
        if (time > delay)
        {
            prevHeight = height;
            if (randomHeight)
            {
                height = UnityEngine.Random.Range(lowerLimit, upperLimit);
            }
            time = 0;
        }

        if (randomHeight == false)
        {
            height = transform.position.y;
        }

        if (randomHeight)
        {
            //Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)  
            transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
        }
        else
        {
            transform.position = new Vector3(transform.position.x, height, transform.position.z);
        }
    }

    private Vector3 GetAverageLocationOfSoliders()
    {
        var total = new Vector3();

        foreach (var soldier in Soldiers)
            total += soldier.transform.position;

        return total / Soldiers.Count();    // Assuming Soldiers is List<Soldier>
    }
}

In the update instead doing as before:

if (time > delay)
            {
                prevHeight = height;
                if (randomHeight)
                {
                    height = UnityEngine.Random.Range(lowerLimit, upperLimit);
                }
                else
                {
                    height = transform.position.y;
                }
                time = 0;
            }

Now I'm doing:

if (time > delay)
        {
            prevHeight = height;
            if (randomHeight)
            {
                height = UnityEngine.Random.Range(lowerLimit, upperLimit);
            }
            time = 0;
        }

        if (randomHeight == false)
        {
            height = transform.position.y;
        }

But I still don't understand why if it's inside the if (time > delay) height is 0 ?

\$\endgroup\$
  • \$\begingroup\$ How much is delay? \$\endgroup\$ – Bálint Jun 2 '18 at 2:11
  • \$\begingroup\$ @Bálint The fact is I didn't understand what the delay is for but I set it to 1 when runinng the game. \$\endgroup\$ – Benzi Avrumi Jun 2 '18 at 2:45
  • \$\begingroup\$ And what are the values of randomHeight, lowerLimit and upperLimit? \$\endgroup\$ – Bálint Jun 2 '18 at 2:48
  • \$\begingroup\$ @Bálint I forgot to mention that transform is main camera and that the camera is child of one of the soldiers and the camera follow the soldiers that are in a move. \$\endgroup\$ – Benzi Avrumi Jun 2 '18 at 2:49
  • 1
    \$\begingroup\$ What engine version are you using? If you are using 2017 or above: After having had a deeper look, it seems that Cinemachine's Freelook Camera is what you want. Go ahead and try it. It's free and integrated into 2018 UPM (unity package manager). \$\endgroup\$ – loptrinho Jun 2 '18 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.