Free move Camera View

I’m in trouble with camera move It always move back to target after camera drag(like move X and Z). then i wonder why there is no target that i did removed target. im very confuse :frowning: I want it like unity camera viewport free move.

i modified MouseOrbit.js here script:

//var target : Transform;
private var distance = 5.0;
private var minDistance = 5.0;
private var maxDistance = 8.0;

private var xSpeed = 250.0;
private var ySpeed = 120.0;

private var yMinLimit = 20;
private var yMaxLimit = 50;

private var x = 0.0;
private var y = 0.0;

function Start() {
	var angles = transform.eulerAngles;
	x = angles.y;
	y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
	var angles = transform.eulerAngles;


    var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 0.0, -distance); //+ target.position;


	if(Input.GetMouseButton(1))
	{
		//if (target) {
			x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
		    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

			y = ClampAngle(y, yMinLimit, yMaxLimit);
			
			transform.rotation = rotation;
	        transform.position = position;			
	//	}
	}
	
	if (Input.GetAxis("Mouse ScrollWheel"))
	{
		distance += Input.GetAxis("Mouse ScrollWheel")*distance;
		distance = Mathf.Clamp(distance, minDistance, maxDistance);

		transform.rotation = rotation;
		transform.position = position;
	}
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

bump… anyone?

Have you taken a look at the Unify Community Wiki? There are a few different camera controller scripts which may suit your needs, perhaps take a look at this one.

Thanks i found some code what im looking in that unify Community Wiki… :slight_smile:

using UnityEngine;
using System.Collections;

public class myView : MonoBehaviour {

    public Transform myCam, playerObj;
    public float mouseX, mouseY, prevMouseX, prevMouseY, xDiff, yDiff, mouseScroll, dist, distToGround;
    public bool isTooClose, isTooFar, isGroundCollision, isBackCollision, hasMoved, lockY;
    public RaycastHit hit;

	void Start () {

        prevMouseX = Input.mousePosition.x * Input.GetAxis("Mouse X");
        prevMouseY = Input.mousePosition.y * Input.GetAxis("Mouse Y");
        mouseX = 0;
        mouseY = 0;
        isTooClose = false;
        isTooFar = false;
        isBackCollision = false;
        lockY = false;
	}
	
	void Update () {

        mouseScroll = Input.GetAxis("Mouse ScrollWheel");
        mouseX = Input.mousePosition.x * Input.GetAxis("Mouse X");
        mouseY = Input.mousePosition.y * Input.GetAxis("Mouse Y");
        xDiff = mouseX - prevMouseX;
        yDiff = mouseY - prevMouseY;

        //rotate around player
        #region rotate
        if (Input.GetMouseButton(2)  xDiff != 0)
        {
            myCam.RotateAround(playerObj.position, Vector3.up, 250* Input.GetAxis("Mouse X") * Time.deltaTime);
        }
        #endregion

        //pan up/down
        #region pan_On_Y
        if (Input.GetMouseButton(2)  yDiff != 0  isGroundCollision == false)
        {

            if (Physics.Raycast(myCam.position, -Vector3.up, out hit, 2))
            {
                isGroundCollision = true;
                return;
            }

            myCam.Translate(Vector3.up * Input.GetAxis("Mouse Y"));
        }

        if (Input.GetMouseButton(2)  yDiff != 0  isGroundCollision == true)
        {

            if (Input.GetAxis("Mouse Y") > 0)
            {
                myCam.Translate(Vector3.up * Input.GetAxis("Mouse Y"));
                isGroundCollision = false;
            }
        }
        #endregion
        //Mouse zoom begin
        #region mouse_zoom
        if (mouseScroll != 0  isTooClose == false  isTooFar == false  isBackCollision == false)
        {

            if (dist < 4.0f)
            {
                isTooClose = true;
                return;
            }

            if (dist > 22.0f)
            {
                isTooFar = true;
                return;
            }

             if (Physics.Raycast(myCam.position, -myCam.forward, out hit, 3))
             {
                 isBackCollision = true;
                 return;
             }

           myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));

        }

        if (mouseScroll != 0  isTooClose == true)
        {
            float temp = Input.GetAxis("Mouse ScrollWheel");

            if (temp < 0)
            {
               myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
                isTooClose = false;
            }
        }

        if (mouseScroll != 0  isTooFar == true)
        {
            float temp = Input.GetAxis("Mouse ScrollWheel");

            if (temp > 0)
            {
                myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
                isTooFar = false;
            }
        }

        if (mouseScroll != 0  isBackCollision == true)
        {
            float temp = Input.GetAxis("Mouse ScrollWheel");

            if (temp > 0)
            {
                myCam.Translate(Vector3.forward * 350 * Time.deltaTime * Input.GetAxis("Mouse ScrollWheel"));
                isBackCollision = false;
            }
        }
        
        #endregion

        Physics.Raycast(myCam.position, -Vector3.up, out hit, 25);

        dist = Vector3.Distance(myCam.position, playerObj.position);
        distToGround = Vector3.Distance(myCam.position, hit.point);
        
        myCam.LookAt(playerObj.position);
        prevMouseX = mouseX;
        prevMouseY = mouseY;
	}

    public void updatePos(Vector3 amount)
    {
        if(lockY == false)
        myCam.position += amount;

        if (lockY == true)
        {
            amount = new Vector3(amount.x, 0, amount.z);
            myCam.position += amount;
        }
    }
}

WOW C#… i hard to understand and read C#…

but why i cant try add for test?

error dialog said “Can’t add script behaviour CameraTEST. The scripts file name does not match the name of the class defined in the script!” ??

With c#, the filename needs to be the same as the classname defined in the file. So the filename needs to be “myView.cs”.

But don’t feel bad if you don’t understand it, it’s not written for others to understand…LoL. Here is another thing about the updatePos() function:

//call this function from your player script. Take the amount the player has moved since the last frame and pass it to updatePos if you want the camera to follow the player. The lockY is because I had an issue with the camera moving down continuously when I jumped. So I set "lockY = true" from my player script when my character jumped. You may or may not needs this.
 public void updatePos(Vector3 amount)
    {
        if(lockY == false)
        myCam.position += amount;

        if (lockY == true)
        {
            amount = new Vector3(amount.x, 0, amount.z);
            myCam.position += amount;
        }
    }

Thanks, Amaz1ng…im really confuse to set up this script but im fine with that i modified mouseOrbit.js it works :slight_smile: