Touch screen and keyboard control on PC

Source: Internet
Author: User
Tags float max

Where target is the reference point of the camera, each shift is made by moving his position to achieve the camera movement, the center of an object is to move the target to the position of the object

1. master Control script

Using Unityengine;
Using Unityengine.ui;
Using System.Collections;


public class Testfocus:monobehaviour
{
Public Text Tex;
Public Transform Ktarget; Camera reference points
public float initdistance = 13.7f; Default distance between camera and reference point
public float Distancemax = 30f; Maximum distance between the camera and the reference point
public float desireddistance; The desired camera distance per operation
public float scrollspeed = 12f; Wheel Movement speed
public float rotatespeed = 8f; Camera Rotation speed
public float panspeed = 0.3f; Drag and drop camera reference movement speed

private float DPI = 0; Speed movement parameters at different resolutions
private float currentdistance = 0f; The current distance of the camera from the reference
private float mousex = 0f; Rotate camera x-week speed
private float mousey = 0f; Speed of rotating the camera y-axis
Private Vector3 velocity = Vector3.zero; Camera reference point Move vector
private float Velocityz = 0f; Speed vector of the camera moving to the target distance point
private float Delayz = 0.3f; Camera Move Buffer time
Private quaternion currentrotation; Camera Current Rotation angle
Private quaternion desiredrotation; The angle the camera expects to reach

private float lastdistance; Achieve a smooth effect of distance and pull away, last camera stop point
private float perdistance; Achieve a smoothing effect that pulls the distance closer and farther away, where each frame of the camera is to be moved

Private Vector2 Endone; Used to record the zoom trend
Private Vector2 Endtwo;

private bool Istouch = false; Whether it is a touch screen


Raycasthit hit;
BOOL _isfocus = false;
Transform Mtrans;

void Awake ()
{
Mtrans = This.transform;
}

Use this for initialization
void Start ()
{
DPI = screen.height/1080f;
Initdistance = Mathf.clamp (initdistance, 1.25f, Distancemax);
Currentdistance = Desireddistance = initdistance;
}

Update is called once per frame
void Lateupdate ()
{

if (_isfocus)
{
Ktarget.position = Vector3.smoothdamp (ktarget.position, hit.transform.position, ref velocity, delayz-0.1f);
Desireddistance = initdistance;

if (Vector3.distance (ktarget.position, hit.transform.position) < 0.0005f)
{
_isfocus = false;
}
}
}


void Update ()
{
if (input.getmousebuttonup (0))
{
Ray Ray = Camera.main.ScreenPointToRay (input.mouseposition);
if (Physics.raycast (Ray, out hit))
{
if (Hit.collider! = null)
{
_isfocus = true;
}
}
}


if (Input.touchcount > 0)
{
if (Input.touchcount = = 1 && input.gettouch (0). Phase = = touchphase.moved)
{
MouseX + = Input.gettouch (0). deltaposition.x * Rotatespeed * DPI * 0.3F;
Mousey-= Input.gettouch (0). DELTAPOSITION.Y * Rotatespeed * DPI * 0.3F;
Tex.text = "in rotation";
}

Touch Screen Zoom
if (Input.touchcount = = 2)
{
if (Input.gettouch (0). Phase = = Touchphase.moved && input.gettouch (1). Phase = = touchphase.moved)
{
Vector2 startone = Input.gettouch (0). Position;
Vector2 starttwo = Input.gettouch (1). Position;
if (Isenlarge (Startone, Starttwo, Endone, Endtwo))
{
Tex.text = "in scale";
if (Desireddistance < Distancemax)
Desireddistance + = DPI * 1f;
}
Else
{
Tex.text = "in magnification";
if (Desireddistance > 5f)
Desireddistance-= DPI * 1f;
}
Endone = Startone;
Endtwo = Starttwo;
}
}

Touch screen for drag-and-drop
if (Input.touchcount = = 3)
{
if (Input.gettouch (0). Phase = = Touchphase.moved && input.gettouch (1). Phase = Touchphase.moved
&& Input.gettouch (2). Phase = = touchphase.moved)
{
Tex.text = "touch screen drag";
Ktarget.rotation = mtrans.rotation;
Ktarget.translate (vector2.right *-input.gettouch (0). deltaposition.x * panspeed);
Ktarget.translate (mtrans.up *-input.gettouch (0). DELTAPOSITION.Y * panspeed);
}
}

Istouch = true;
}

Else
{
Istouch = false;
}

Keyboard operation, rotation and drag
if (Input.getmousebutton (0) &&!istouch)
{
_isfocus = false;
MouseX + = Input.getaxis ("Mouse X") * rotatespeed;
Mousey-= Input.getaxis ("Mouse Y") * rotatespeed;
Tex.text = "in keyboard rotation";

}

if (Input.getmousebutton (1) &&!istouch)
{
_isfocus = false;
Ktarget.rotation = mtrans.rotation;
Ktarget.translate (Vector2.right *-input.getaxis ("Mouse X") * panspeed);
Ktarget.translate (Mtrans.up *-input.getaxis ("Mouse Y") * panspeed, Space.world);
}
Ktarget.position = new Vector3 (Mathf.clamp (ktarget.position.x, -15f, 15f), Mathf.clamp (KTARGET.POSITION.Y, -10f, 10f), Mathf.clamp (Ktarget.position.z, -30f, 30f));


Mousey = Clampangle (Mousey, -30f, 30f);


Keyboard scaling along the z axis
float Scrollvalue = Input.getaxis ("Mouse scrollwheel");
if (Scrollvalue! = 0)
{
_isfocus = false;
Desireddistance-= Scrollvalue * scrollspeed;
Tex.text = "Keyboard control distance closer or farther away";
}


Limit distance
if (Desireddistance > Distancemax)
Desireddistance = Distancemax;

if (Desireddistance < 1.25f)
{
Desireddistance = 1.25f;
}

Currentrotation = mtrans.rotation;
Desiredrotation = Quaternion.euler (mousey, MouseX, 0);

quaternion rotation = Quaternion.slerp (currentrotation, desiredrotation, Time.deltatime * 15f);


Perdistance = desireddistance;
Perdistance = Mathf.smoothdamp (lastdistance, perdistance, ref velocityz, Delayz);
Lastdistance = perdistance;

Vector3 position = rotation * New Vector3 (0f, 0f,-perdistance) + ktarget.position;
Mtrans.rotation = rotation;
Mtrans.position = position;

}

float Clampangle (float angle, float min, float max)
{
if (angle <-360)
Angle + = 360;
if (Angle > 360)
Angle-= 360;
return Mathf.clamp (angle, Min, max);
}

BOOL Isenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
float length1 = mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (OP1.Y-OP2.Y));
float length2 = mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (NP1.Y-NP2.Y));

if (Length1 < length2)
{
return true;
}
Else
{
return false;
}
}
}

Touch screen and keyboard control on PC

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.