script building: Click and drag to rotate view
#1
Posted 17 April 2012 - 02:44 PM
The goal of this script is to rotate around the parent object, or object determined by script (GameObject) when the mouse is click-dragged and once released to slowdown over time.delta for 0.5f
I'll look at the rotate mouse script and pull it apart soon, as for now, I'm watching Game of Thrones.
RIP 2323
#2
Posted 05 May 2012 - 02:59 AM
modified from the original and finished it,as the original script wasn't successfully completed
attach to camera, controls are as follows, Scroll wheel is zoom, middle click is Pan (disabled) and left click and drag is rotate
// //Filename: maxCamera.cs // // original: http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitZoom // // --01-18-2010 - create temporary target, if none supplied at start using UnityEngine; using System.Collections; [AddComponentMenu("Camera-Control/3dsMax Camera Style")] public class maxCamera : MonoBehaviour { public Transform target; public Vector3 targetOffset; public float distance = 5.0f; public float maxDistance = 20; public float minDistance = .6f; public float xSpeed = 200.0f; public float ySpeed = 200.0f; public int yMinLimit = -80; public int yMaxLimit = 80; public int zoomRate = 40; public float panSpeed = 0.3f; public float zoomDampening = 5.0f; private float xDeg = 0.0f; private float yDeg = 0.0f; private float currentDistance; private float desiredDistance; private Quaternion currentRotation; private Quaternion desiredRotation; private Quaternion rotation; private Vector3 position; private float isOrbiting = 0; void Start() { Init(); } void OnEnable() { Init(); } public void Init() { //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint if (!target) { GameObject go = new GameObject("Cam Target"); go.transform.position = transform.position + (transform.forward * distance); target = go.transform; } distance = Vector3.Distance(transform.position, target.position); currentDistance = distance; desiredDistance = distance; //be sure to grab the current rotations as starting points. position = transform.position; rotation = transform.rotation; currentRotation = transform.rotation; desiredRotation = transform.rotation; xDeg = Vector3.Angle(Vector3.right, transform.right ); yDeg = Vector3.Angle(Vector3.up, transform.up ); } /* * Camera logic on LateUpdate to only update after all character movement logic has been handled. */ void LateUpdate() { // ZOOM! if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl)) { desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate*0.125f * Mathf.Abs(desiredDistance); } //ORBIT if (Input.GetMouseButton(0) /*&& Input.GetKey(KeyCode.LeftAlt)*/) { isOrbiting = 1; } else { isOrbiting = 0; } xDeg += Input.GetAxis("Mouse X") * (xSpeed * isOrbiting) * 0.02f; yDeg -= Input.GetAxis("Mouse Y") * (ySpeed * isOrbiting) * 0.02f; //Clamp the vertical axis for the orbit yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit); // set camera rotation desiredRotation = Quaternion.Euler(yDeg, xDeg, 0); currentRotation = transform.rotation; rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening); transform.rotation = rotation; /* Remove Pan // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace else if (Input.GetMouseButton(2)) { //grab the rotation of the camera so we can move in a psuedo local XY space target.rotation = transform.rotation; target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed); target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World); } */ ////////Update Orbit // affect the desired Zoom distance if we roll the scrollwheel desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); //clamp the zoom min/max desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance); // For smoothing of the zoom, lerp distance currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening); // calculate position based on the new currentDistance position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset); transform.position = position; } private static float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); } }
Enjoy
RIP 2323
#3
Posted 05 May 2012 - 01:58 PM
(Also, dunno why you posted this thread in Grymleif and not the library, might move it over there if that suits you.)
Anyway, I did a camera script that sounds somewhat like yours back in the day(Though my coding competence has grown since then so the code is probably ugly). It was done in JS though, and is a lot longer than yours, but fun to see someone else trying things.
Just booted up your script, and it certainly feels quite smooth. Going to be looking at that one when I make some new camera scripts in the future.
Another question: What is your personal reason to write things in c# vs boo/JS? prior experience or just more sensible code? I've got little to no experience with c#, but its been said to be windows version of javascript, and it looks easy enough imo.
Edited by duke_Qa, 05 May 2012 - 01:59 PM.
"I give you private information on corporations for free and I'm a villain. Mark Zuckerberg gives your private information to corporations for money and he's 'Man of the Year.'" - Assange
#4
Posted 05 May 2012 - 04:11 PM
personally i prefer Js over ease of coding, but for more robust code, or things that need to be a bit more powerful I'd rather slip it in as C#
my coding experience comes from action-script so Java is more familiar, but in the end of the day C# seems to be a little more stable then Js and isn't such an impact on the machine since is more native, but the difference is so thin its no real impact.
basically, JavaScript, is easier to code, and C# is a little more strict and formal.
Edited by DIGI_Byte, 05 May 2012 - 04:11 PM.
RIP 2323
#5
Posted 05 May 2012 - 08:26 PM
I'll just see if I can move this thing around a bit. You and I seem to have much the same focus fields at times, are you working with this stuff as a dayjob, or just on hobby basis?
edit: tried the panning one now. I think there should be a speed multiplier that varies with the distance from the object + perspective of the camera, and something of a momentum/floating stop if you "throw" the camera in a direction, like Photoshop added in the later editions. Not important I guess but something to think about, might add it if I steal the code for some testing once. .
Edited by duke_Qa, 06 May 2012 - 02:59 PM.
"I give you private information on corporations for free and I'm a villain. Mark Zuckerberg gives your private information to corporations for money and he's 'Man of the Year.'" - Assange
#6
Posted 14 May 2012 - 01:16 AM
Long story short i was enjoying coding more then animation, i got the basic degree so why push it further when a portfolio is good enough, besides, I dont want to be a 'master' in something i have no true interest in, else people would rely on me for it.
as for the pan, maybe if you use the same smooth multiplier but im not sure if that will work, haven't tested it but shouldn't be to hard other then copy paste from one part to another. If you get it working, post it here.
no sense missing out on one hell of a valuable script
Maybe we could team up? and share scripts etc. at the end of the day unity is a wonderful blank canvas you really need to think about the basis on how the game is to be built, I did 3 full iterations and now i think i got something more realistically workable. But im stuck with another part.
RIP 2323
#7
Posted 14 May 2012 - 08:15 AM
Interactive work feels much more rewarding for me, like solving artistic puzzles. I guess I should have gotten all the math and the things that I lack(all the higher level math, more pattern knowledge, more advanced programming functions beyond for-loops and epsilon and bitwise << or >> operators), but if there is one thing I've learned from creative studies it is that there will always be "someone" who are better than yourself. The trick is to accept that your competence is also valuable even if it isn't as godlike as some others. Being good in both 3d and programming can be a good combination.
Anyway, I don't mind teaming up on sharing code and the likes in this place. If you want to do some other unity3d projects later on I might be up for it, barring any other projects I've promised to help out on at the same time. I'm just glad there's some life in this part of the Revora forums now. I hope it will continue to grow, because unity is a very good engine for all kinds of "mod"-like things.
"I give you private information on corporations for free and I'm a villain. Mark Zuckerberg gives your private information to corporations for money and he's 'Man of the Year.'" - Assange
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users