Home > Software engineering >  C# script to return camera to a gameobject
C# script to return camera to a gameobject

Time:12-21

in Unity I have a camera as a child of a 2D gameobject (to follow it around). There is an IF statement that lets me move the camera ahead by holding down a key. I need a code to return the camera back to the gameobject after I let go. Thank you for the help.

public class camera : MonoBehaviour
{
    public float panspeed = 30f;
    public float panBorderThickness = 30f;
    public GameObject ship1;
    private Vector3 offset;
    

    void Update()
    {
        if (Input.GetKey("f"))
        {
            Vector3 pos = transform.position;
            if (Input.mousePosition.y >= Screen.height - panBorderThickness)
            {
                pos.y  = panspeed * Time.deltaTime;
            }
            if (Input.mousePosition.y <= panBorderThickness)
            {
                pos.y -= panspeed * Time.deltaTime;
            }
            if (Input.mousePosition.x >= Screen.width - panBorderThickness)
            {
                pos.x  = panspeed * Time.deltaTime;
            }
            if (Input.mousePosition.x <= panBorderThickness)
            {
                pos.x -= panspeed * Time.deltaTime;
            }
            transform.position = pos;
        }
        //something to return the camera back when i let go of F key
    }
}

CodePudding user response:

if(Input.GetKeyUp("f")){
    transform.position = ship1.transform.position;
}

Sidenote, you can't assign values to only one component of a vector. You need to reassign the entire vector with the new component or add/subtract to the original vector. Your script should look like

if (Input.GetKey("f"))
{
    Vector3 pos = transform.position;
    if (Input.mousePosition.y >= Screen.height - panBorderThickness)
    {
        pos  = new Vector3(0, panspeed * Time.deltaTime, 0);
    }
    if (Input.mousePosition.y <= panBorderThickness)
    {
        pos -= new Vector3(0, panspeed * Time.deltaTime, 0);
    }
    if (Input.mousePosition.x >= Screen.width - panBorderThickness)
    {
        pos  = new Vector3(panspeed * Time.deltaTime, 0, 0);
    }
    if (Input.mousePosition.x <= panBorderThickness)
    {
        pos -= new Vector3(panspeed * Time.deltaTime, 0, 0);
    }
    transform.position = pos;
}

if(Input.GetKeyUp("f"))
{
    transform.position = GameObjectYouWant.transform.position;
}

CodePudding user response:

If I understand correctly you would want to smoothly move the camera back to its original position so I would probably do it like

private Vector3 originalPosition;

private void Start ()
{
    originalPosition = transform.position;
}

private void Update ()
{
    if (Input.GetKey("f"))
    {
        var pos = transform.position;
        if (Input.mousePosition.y >= Screen.height - panBorderThickness)
        {
            pos.y  = panspeed * Time.deltaTime;
        }
        if (Input.mousePosition.y <= panBorderThickness)
        {
            pos.y -= panspeed * Time.deltaTime;
        }
        if (Input.mousePosition.x >= Screen.width - panBorderThickness)
        {
            pos.x  = panspeed * Time.deltaTime;
        }
        if (Input.mousePosition.x <= panBorderThickness)
        {
            pos.x -= panspeed * Time.deltaTime;
        }
        transform.position = pos;
    }
    else
    {
        transform.position = Vector3.MoveTowards(transform.position, originalPosition, panspeed * Time.deltaTime);
    }
}
  • Related