Friday, 4 March 2016

Updated Code

___________________________________________________________________________________________________

Updated Code to fix camera:

using UnityEngine;
using System.Collections;
using System;

[RequireComponent(typeof(Camera))]
public class CameraMovement : MonoBehaviour {

    public float _width = 200.0f; //width and height of camera movement
    public float _height = 200.0f;

    public float _movementDamping = 1.0f; //this reduces movement

    private Vector3 _currentVelocity;
    private Camera _camera;

    private void Start()
    {
        _camera = GetComponent<Camera>();
        _currentVelocity = new Vector3(0.0f, 0.0f, 0.0f);
    }
    private void Update() // this update stops the camera from flying off into space
    {
        float aspectRatio = (float)Screen.width / (float)Screen.height;

        Vector3 targetPosition = new Vector3(
            (Input.GetAxis("Mouse X"))*100.0f,
            (Input.GetAxis("Mouse Y"))*100.0f,
            transform.position.z);

        if (targetPosition.x > _width / 2)
            targetPosition.x = _width / 2;
        if (targetPosition.x < -_width / 2)
            targetPosition.x = -_width / 2;
        if (targetPosition.y > _height / 2)
            targetPosition.y = _height / 2;
        if (targetPosition.y < -_height / 2)
            targetPosition.y = -_height / 2;

        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref _currentVelocity, _movementDamping);
    }
}

0 comments:

Post a Comment