Unity for in-game camera view Control

Source: Internet
Author: User
Tags float max

Using unityengine;using System.collections;public class Newcamera:monobehaviour {public Gameobject cameraFather; Public Transform target;public Float rotatespeed;public float scalespeed;public float min;public float max;public transfo RM obj;public Float cameraspeed;private bool Islock =false;private Vector3 fcurrentrotation;private float Currentsacale; Private Vector3 currentrotation;private Vector3 lastmouseposition;//Use this for initializationvoid Start () {Fcurrentro tation = Camerafather.transform.eulerangles;currentrotation = Transform.eulerangles;lastmouseposition = Input.mouseposition;} Update is called once per framevoid Update () {if (obj! = null && input.getkeydown (keycode.space) && is Lock = = False) {Islock = true;} else if (Input.getkeydown (Keycode.space)) {Islock = false;} if (islock) {cameraFather.transform.position = obj.position;} else if (! Input.getmousebutton (0)) {if (input.mouseposition.x <= screen.width && input.mouseposition.x >= Screen.width-60) {cameraFather.transform.Translate (new Vector3 (1,0,0) *cameraspeed*time. deltatime);} else if (input.mouseposition.x >= 0 && input.mouseposition.x <=) {cameraFather.transform.Translate (new Vector3 (1,0,0) *-cameraspeed*time. deltatime);} if (input.mouseposition.y >= 0 && input.mouseposition.y <=) {cameraFather.transform.Translate (new Vector3 (0,0,1) *-cameraspeed*time. deltatime);} else if (input.mouseposition.y >= screen.height-60 && input.mouseposition.y <= screen.height) { CameraFather.transform.Translate (New Vector3 (0,0,1) *cameraspeed*time. deltatime);}} Currentsacale = 0;//transform. LookAt (camerachild.position); Vector3 Mousedelta = Input.mouseposition-lastmouseposition;lastmouseposition = Input.mouseposition;if ( Input.getmousebutton (0)) {FCURRENTROTATION.Y + = mousedelta.x * Rotatespeed * time.deltatime;currentrotation.x + = MOUSEDELTA.Y * rotatespeed * time.deltatime;currentrotation.y + mousedelta.x * rotatespeed * Time.deltaTime;} Currentsacale = input.mousescrolldelta.y * scalespeed * TIME.DELTATIME;IF (Currentsacale! = 0) CurrentSacale = Mathf.Clamp (Currentsacale,min,max);D ebug. Log (currentsacale); transform.position = Target.position;camerafather.transform.eulerangles = fCurrentRotation; Transform.eulerangles = Currentrotation;transform. Translate (New Vector3 (0,0,currentsacale));}}

 

1. Create an empty object, pull it (gameobject) into the object folder to be watched by the camera, and the object is like a hero League hero.
2. Clear the position of the transform column 0;
3. Then pull out the empty object and put it to the camera folder, the rotation of the transform column will be cleared 0;
4. Pull the empty object out to the camera folder on the same layer, the empty object transform the rotation X-clear 0;
5. Pull the camera into the empty object folder.
6. Add the NewCamera.cs to the camera.
7. Pull the empty object to the Camerafather option, pull the camera to Target,rotateseed is the rotation speed, enter a positive number yourself, scalespeed roller drive the camera movement speed, Min and Max are scalespeed size limits, Min is generally negative, and Max takes positive values. (Less than the Min value is greater than the max value). Pull the observed object to the OBJ option. Cameraspeed is the speed at which the mouse moves near the edge of the screen.

Key instructions
Press Space (SPACEBAR) to lock the view
Press and hold the left mouse button to drag the rotation angle
Roller change camera and object distance

 

Unity for in-game camera view Control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.