RPG role Camera Control

Source: Internet
Author: User
Tags float max

Using Unityengine;
Using System.Collections;

public class Rpg_camera:monobehaviour {

public static Rpg_camera instance;

Public Transform camerapivot;//The position reference point of the relative character, which can be at the top of the task head
public float distance = 5f;
public float Distancemax = 30f;
public float mousespeed = 8f;
public float mousescroll = 15f;
public float mousesmoothingfactor = 0.08f;
public float camdistancespeed = 0.7f;
public float cambottomdistance = 1f;
public float firstpersonthreshold = 0.8f;
public float characterfadethreshold = 1.8f;

Private Vector3 desiredposition;
private float desireddistance;
private float lastdistance;
private float mousex = 0f;
private float Mousexsmooth = 0f;
private float Mousexvel;
private float mousey = 0f;
private float Mouseysmooth = 0f;
private float Mouseyvel;
private float mouseymin = -89.5f;
private float Mouseymax = 89.5f;
private float Distancevel;
private bool Cambottom;
private bool constraint;

private static float Halffieldofview;
private static float Planeaspect;
private static float halfplaneheight;
private static float halfplanewidth;

void Awake () {
instance = this;
}


void Start () {
Distance = Mathf.clamp (distance, 0.05f, Distancemax);
Desireddistance = distance;

Halffieldofview = (CAMERA.MAIN.FIELDOFVIEW/2) * MATHF.DEG2RAD;
Planeaspect = Camera.main.aspect;
Halfplaneheight = Camera.main.nearClipPlane * Mathf.tan (Halffieldofview);
Halfplanewidth = Halfplaneheight * planeaspect;

MouseX = 0f;
Mousey = 15f;
}


public static void Camerasetup () {
Gameobject cameraused;
Gameobject Camerapivot;
Rpg_camera Camerascript;

if (camera.main! = null)
cameraused = Camera.main.gameObject;
else {
cameraused = new Gameobject ("Main Camera");
Cameraused.addcomponent<camera> ();
Cameraused.tag = "Maincamera";
}

if (!cameraused.getcomponent ("Rpg_camera"))
Cameraused.addcomponent<rpg_camera> ();
Camerascript = Cameraused.getcomponent ("Rpg_camera") as Rpg_camera;

Camerapivot = Gameobject.find ("Camerapivot") as Gameobject;
Camerascript.camerapivot = Camerapivot.transform;
}


void Lateupdate () {
if (Camerapivot = = null) {
Debug.Log ("Error:no camerapivot found! Please read the manual for further instructions. ");
Return
}

GetInput ();

Getdesiredposition ();

Positionupdate ();

Characterfade ();
}


void GetInput () {

if (Distance > 0.1) {//distance > 0.05 would be too close, so 0.1 are fine
Debug.drawline (transform.position, Transform.position-vector3.up * cambottomdistance, Color.green);
Cambottom = Physics.linecast (transform.position, transform.position-vector3.up * cambottomdistance);
}

BOOL Constrainmousey = cambottom && transform.position.y-camerapivot.transform.position.y <= 0;

if (Input.getmousebutton (0) | | Input.getmousebutton (1)) {
Cursor.visible = false; If you want the cursor behavior of the version 1.0, change this line to "screen.lockcursor = true;"

MouseX + = Input.getaxis ("Mouse X") * mousespeed;

if (Constrainmousey) {
if (Input.getaxis ("Mouse Y") < 0)
Mousey-= Input.getaxis ("Mouse Y") * mousespeed;
} else
Mousey-= Input.getaxis ("Mouse Y") * mousespeed;
} else
Cursor.visible = true; If you want the cursor behavior of the version 1.0, change this line to "screen.lockcursor = false;"

Mousey = Clampangle (Mousey, -89.5f, 89.5f);
Mousexsmooth = Mathf.smoothdamp (Mousexsmooth, MouseX, ref mousexvel, Mousesmoothingfactor);
Mouseysmooth = Mathf.smoothdamp (Mouseysmooth, Mousey, ref mouseyvel, Mousesmoothingfactor);

if (Constrainmousey)
Mouseymin = Mousey;
Else
Mouseymin = -89.5f;

Mouseysmooth = Clampangle (Mouseysmooth, Mouseymin, Mouseymax);


if (Input.getmousebutton (1))
RPG_Controller.instance.transform.rotation = Quaternion.euler (rpg_controller.instance.transform.eulerangles.x, Camera.main.transform.eulerangles.y, rpg_controller.instance.transform.eulerangles.z);

Desireddistance = Desireddistance-input.getaxis ("Mouse scrollwheel") * mousescroll;

if (Desireddistance > Distancemax)
Desireddistance = Distancemax;

if (Desireddistance < 0.05)
Desireddistance = 0.05f;
}


void Getdesiredposition () {
Distance = desireddistance;
Desiredposition = Getcameraposition (Mouseysmooth, mousexsmooth, distance);

float closestdistance;
constraint = false;

Closestdistance = Checkcameraclipplane (camerapivot.position, desiredposition);

if (closestdistance! =-1) {
Distance = closestdistance;
Desiredposition = Getcameraposition (Mouseysmooth, mousexsmooth, distance);

Constraint = true;
}


Distance-= Camera.main.nearClipPlane;

if (Lastdistance < distance | |!constraint)
Distance = Mathf.smoothdamp (lastdistance, distance, ref distancevel, camdistancespeed);

if (Distance < 0.05)
Distance = 0.05f;

Lastdistance = distance;

Desiredposition = Getcameraposition (Mouseysmooth, mousexsmooth, distance); If the camera view was blocked and then this is the new "forced" position
}


void Positionupdate () {
Transform.position = desiredposition;

if (Distance > 0.05)
Transform. LookAt (Camerapivot);
}


void Characterfade () {
if (rpg_animation.instance = = null)
Return

if (Distance < firstpersonthreshold)
Rpg_animation.instance.getcomponent<renderer> (). Enabled = FALSE;

else if (distance < Characterfadethreshold) {
Rpg_animation.instance.getcomponent<renderer> (). Enabled = TRUE;

float Characteralpha = 1-(characterfadethreshold-distance)/(Characterfadethreshold-firstpersonthreshold);
if (rpg_animation.instance.getcomponent<renderer> (). material.color.a! = Characteralpha)
Rpg_animation.instance.getcomponent<renderer> (). Material.color = new Color (rpg_ Animation.instance.getcomponent<renderer> (). MATERIAL.COLOR.R, rpg_animation.instance.getcomponent< Renderer> (). MATERIAL.COLOR.G, Rpg_animation.instance.getcomponent<renderer> (). Material.color.b, Characteralpha);

} else {

Rpg_animation.instance.getcomponent<renderer> (). Enabled = TRUE;

if (rpg_animation.instance.getcomponent<renderer> (). material.color.a! = 1)
Rpg_animation.instance.getcomponent<renderer> (). Material.color = new Color (rpg_ Animation.instance.getcomponent<renderer> (). MATERIAL.COLOR.R, rpg_animation.instance.getcomponent< Renderer> (). MATERIAL.COLOR.G, Rpg_animation.instance.getcomponent<renderer> (). material.color.b, 1);
}
}


Vector3 getcameraposition (float xaxis, float yaxis, float distance) {
Vector3 offset = new Vector3 (0, 0,-distance);
quaternion rotation = Quaternion.euler (Xaxis, YAxis, 0);
return camerapivot.position + rotation * OFFSET;
}


Float Checkcameraclipplane (Vector3 from, Vector3 to) {
var closestdistance = -1f;

Raycasthit Hitinfo;

Clipplanevertexes Clipplane = Getclipplaneat (To);

Debug.drawline (Clipplane.upperleft, clipplane.upperright);
Debug.drawline (Clipplane.upperright, clipplane.lowerright);
Debug.drawline (Clipplane.lowerright, clipplane.lowerleft);
Debug.drawline (Clipplane.lowerleft, clipplane.upperleft);

Debug.drawline (from, to, color.red);
Debug.drawline (From-transform.right * halfplanewidth + transform.up * halfplaneheight, ClipPlane.UpperLeft, Color.cyan );
Debug.drawline (from + transform.right * halfplanewidth + transform.up * halfplaneheight, Clipplane.upperright, Color . Cyan);
Debug.drawline (from-transform.right * halfplanewidth-transform.up * halfplaneheight, ClipPlane.LowerLeft, Color. Cyan);
Debug.drawline (from + transform.right * halfplanewidth-transform.up * halfplaneheight, Clipplane.lowerright, Color . Cyan);


if (Physics.linecast (from, to, out hitinfo) && HitInfo.collider.tag! = "Player")
Closestdistance = Hitinfo.distance-camera.main.nearclipplane;

if (Physics.linecast (from-transform.right * halfplanewidth + transform.up * halfplaneheight, clipPlane.UpperLeft, out hi tinfo) && HitInfo.collider.tag! = "Player")
if (Hitinfo.distance < Closestdistance | | closestdistance = =-1)
Closestdistance = vector3.distance (hitinfo.point + transform.right * halfplanewidth-transform.up * halfPlaneHeight, fro m);

if (Physics.linecast (from + transform.right * halfplanewidth + transform.up * halfplaneheight, clipplane.upperright, out H Itinfo) && HitInfo.collider.tag! = "Player")
if (Hitinfo.distance < Closestdistance | | closestdistance = =-1)
Closestdistance = vector3.distance (hitinfo.point-transform.right * halfplanewidth-transform.up * halfPlaneHeight, fro m);

if (Physics.linecast (from-transform.right * halfplanewidth-transform.up * halfplaneheight, clipPlane.LowerLeft, out hi tinfo) && HitInfo.collider.tag! = "Player")
if (Hitinfo.distance < Closestdistance | | closestdistance = =-1)
Closestdistance = vector3.distance (hitinfo.point + transform.right * halfplanewidth + transform.up * halfPlaneHeight, fro m);

if (Physics.linecast (from + transform.right * halfplanewidth-transform.up * halfplaneheight, clipplane.lowerright, out H Itinfo) && HitInfo.collider.tag! = "Player")
if (Hitinfo.distance < Closestdistance | | closestdistance = =-1)
Closestdistance = vector3.distance (hitinfo.point-transform.right * halfplanewidth + transform.up * halfPlaneHeight, fro m);


return closestdistance;
}


float Clampangle (float angle, float min, float max) {
while (Angle < -360 | | angle > 360) {
if (angle <-360)
Angle + = 360;
if (Angle > 360)
Angle-= 360;
}

return Mathf.clamp (angle, Min, max);
}


public struct Clipplanevertexes {
Public Vector3 Upperleft;
Public Vector3 upperright;
Public Vector3 Lowerleft;
Public Vector3 lowerright;
}


public static clipplanevertexes Getclipplaneat (Vector3 Pos) {
var clipplane = new Clipplanevertexes ();

if (Camera.main = = null)
return clipplane;

Transform Transform = Camera.main.transform;
float offset = Camera.main.nearClipPlane;

Clipplane.upperleft = Pos-transform.right * halfplanewidth;
Clipplane.upperleft + = Transform.up * halfplaneheight;
Clipplane.upperleft + = Transform.forward * OFFSET;

Clipplane.upperright = pos + transform.right * halfplanewidth;
Clipplane.upperright + = Transform.up * halfplaneheight;
Clipplane.upperright + = Transform.forward * OFFSET;

Clipplane.lowerleft = Pos-transform.right * halfplanewidth;
Clipplane.lowerleft-= Transform.up * halfplaneheight;
Clipplane.lowerleft + = Transform.forward * OFFSET;

Clipplane.lowerright = pos + transform.right * halfplanewidth;
Clipplane.lowerright-= Transform.up * halfplaneheight;
Clipplane.lowerright + = Transform.forward * OFFSET;


return clipplane;
}


public void Rotatewithcharacter () {
float rotation = Input.getaxis ("horizontal") * RPG_Controller.instance.turnSpeed;
MouseX + = rotation;
}
}

RPG role Camera 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.