Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.12 KB | None | 0 0
  1. #pragma strict
  2.  
  3. // Objects to drag in
  4. public var motor : MovementMotor;
  5. public var character : Transform;
  6. public var cursorPrefab : GameObject;
  7. public var joystickPrefab : GameObject;
  8.  
  9. // Settings
  10. public var cameraSmoothing : float = 0.01;
  11. public var cameraPreview : float = 2.0f;
  12.  
  13. // Cursor settings
  14. public var cursorPlaneHeight : float = 0;
  15. public var cursorFacingCamera : float = 0;
  16. public var cursorSmallerWithDistance : float = 0;
  17. public var cursorSmallerWhenClose : float = 1;
  18.  
  19. // Private memeber data
  20. private var mainCamera : Camera;
  21.  
  22. private var cursorObject : Transform;
  23. private var joystickLeft : Joystick;
  24. private var joystickRight : Joystick;
  25.  
  26. private var mainCameraTransform : Transform;
  27. private var cameraVelocity : Vector3 = Vector3.zero;
  28. private var cameraOffset : Vector3 = Vector3.zero;
  29. private var initOffsetToPlayer : Vector3;
  30.  
  31. // Prepare a cursor point varibale. This is the mouse position on PC and controlled by the thumbstick on mobiles.
  32. private var cursorScreenPosition : Vector3;
  33.  
  34. private var playerMovementPlane : Plane;
  35.  
  36. private var joystickRightGO : GameObject;
  37.  
  38. private var screenMovementSpace : Quaternion;
  39. private var screenMovementForward : Vector3;
  40. private var screenMovementRight : Vector3;
  41.  
  42. function Awake () {    
  43.     motor.movementDirection = Vector2.zero;
  44.     motor.facingDirection = Vector2.zero;
  45.    
  46.     // Set main camera
  47.     mainCamera = Camera.main;
  48.     mainCameraTransform = mainCamera.transform;
  49.    
  50.     // Ensure we have character set
  51.     // Default to using the transform this component is on
  52.     if (!character)
  53.         character = transform;
  54.    
  55.     initOffsetToPlayer = mainCameraTransform.position - character.position;
  56.    
  57.     #if UNITY_IPHONE || UNITY_ANDROID
  58.         if (joystickPrefab) {
  59.             // Create left joystick
  60.             var joystickLeftGO : GameObject = Instantiate (joystickPrefab) as GameObject;
  61.             joystickLeftGO.name = "Joystick Left";
  62.             joystickLeft = joystickLeftGO.GetComponent.<Joystick> ();
  63.            
  64.             // Create right joystick
  65.             joystickRightGO = Instantiate (joystickPrefab) as GameObject;
  66.             joystickRightGO.name = "Joystick Right";
  67.             joystickRight = joystickRightGO.GetComponent.<Joystick> ();        
  68.         }
  69.     #else
  70.         if (cursorPrefab) {
  71.             cursorObject = (Instantiate (cursorPrefab) as GameObject).transform;
  72.         }
  73.     #endif
  74.    
  75.     // Save camera offset so we can use it in the first frame
  76.     cameraOffset = mainCameraTransform.position - character.position;
  77.    
  78.     // Set the initial cursor position to the center of the screen
  79.     cursorScreenPosition = Vector3 (0.5 * Screen.width, 0.5 * Screen.height, 0);
  80.    
  81.     // caching movement plane
  82.     playerMovementPlane = new Plane (character.up, character.position + character.up * cursorPlaneHeight);
  83. }
  84.  
  85. function Start () {
  86.     #if UNITY_IPHONE || UNITY_ANDROID
  87.         // Move to right side of screen
  88.         var guiTex : GUITexture = joystickRightGO.GetComponent.<GUITexture> ();
  89.         guiTex.pixelInset.x = Screen.width - guiTex.pixelInset.x - guiTex.pixelInset.width;        
  90.     #endif 
  91.    
  92.     // it's fine to calculate this on Start () as the camera is static in rotation
  93.    
  94.     screenMovementSpace = Quaternion.Euler (0, mainCameraTransform.eulerAngles.y, 0);
  95.     screenMovementForward = screenMovementSpace * Vector3.forward;
  96.     screenMovementRight = screenMovementSpace * Vector3.right; 
  97. }
  98.  
  99. function OnDisable () {
  100.     if (joystickLeft)
  101.         joystickLeft.enabled = false;
  102.    
  103.     if (joystickRight)
  104.         joystickRight.enabled = false;
  105. }
  106.  
  107. function OnEnable () {
  108.     if (joystickLeft)
  109.         joystickLeft.enabled = true;
  110.    
  111.     if (joystickRight)
  112.         joystickRight.enabled = true;
  113. }
  114.  
  115. function Update () {
  116.     // HANDLE CHARACTER MOVEMENT DIRECTION
  117.     #if UNITY_IPHONE || UNITY_ANDROID
  118.         motor.movementDirection = joystickLeft.position.x * screenMovementRight + joystickLeft.position.y * screenMovementForward;
  119.     #else
  120.         motor.movementDirection = Input.GetAxis ("Horizontal") * screenMovementRight + Input.GetAxis ("Vertical") * screenMovementForward;
  121.     #endif
  122.    
  123.     // Make sure the direction vector doesn't exceed a length of 1
  124.     // so the character can't move faster diagonally than horizontally or vertically
  125.     if (motor.movementDirection.sqrMagnitude > 1)
  126.         motor.movementDirection.Normalize();
  127.    
  128.    
  129.     // HANDLE CHARACTER FACING DIRECTION AND SCREEN FOCUS POINT
  130.    
  131.     // First update the camera position to take into account how much the character moved since last frame
  132.     //mainCameraTransform.position = Vector3.Lerp (mainCameraTransform.position, character.position + cameraOffset, Time.deltaTime * 45.0f * deathSmoothoutMultiplier);
  133.    
  134.     // Set up the movement plane of the character, so screenpositions
  135.     // can be converted into world positions on this plane
  136.     //playerMovementPlane = new Plane (Vector3.up, character.position + character.up * cursorPlaneHeight);
  137.    
  138.     // optimization (instead of newing Plane):
  139.    
  140.     playerMovementPlane.normal = character.up;
  141.     playerMovementPlane.distance = -character.position.y + cursorPlaneHeight;
  142.    
  143.     // used to adjust the camera based on cursor or joystick position
  144.    
  145.     var cameraAdjustmentVector : Vector3 = Vector3.zero;
  146.    
  147.     #if UNITY_IPHONE || UNITY_ANDROID
  148.    
  149.         // On mobiles, use the thumb stick and convert it into screen movement space
  150.         motor.facingDirection = joystickRight.position.x * screenMovementRight + joystickRight.position.y * screenMovementForward;
  151.                
  152.         cameraAdjustmentVector = motor.facingDirection;    
  153.    
  154.     #else
  155.    
  156.         #if !UNITY_EDITOR && (UNITY_XBOX360 || UNITY_PS3)
  157.  
  158.             // On consoles use the analog sticks
  159.             var axisX : float = Input.GetAxis("LookHorizontal");
  160.             var axisY : float = Input.GetAxis("LookVertical");
  161.             motor.facingDirection = axisX * screenMovementRight + axisY * screenMovementForward;
  162.    
  163.             cameraAdjustmentVector = motor.facingDirection;    
  164.        
  165.         #else
  166.    
  167.             // On PC, the cursor point is the mouse position
  168.             var cursorScreenPosition : Vector3 = Input.mousePosition;
  169.                        
  170.             // Find out where the mouse ray intersects with the movement plane of the player
  171.             var cursorWorldPosition : Vector3 = ScreenPointToWorldPointOnPlane (cursorScreenPosition, playerMovementPlane, mainCamera);
  172.            
  173.             var halfWidth : float = Screen.width / 2.0f;
  174.             var halfHeight : float = Screen.height / 2.0f;
  175.             var maxHalf : float = Mathf.Max (halfWidth, halfHeight);
  176.            
  177.             // Acquire the relative screen position        
  178.             var posRel : Vector3 = cursorScreenPosition - Vector3 (halfWidth, halfHeight, cursorScreenPosition.z);     
  179.             posRel.x /= maxHalf;
  180.             posRel.y /= maxHalf;
  181.                        
  182.             cameraAdjustmentVector = posRel.x * screenMovementRight + posRel.y * screenMovementForward;
  183.             cameraAdjustmentVector.y = 0.0;
  184.                                    
  185.             // The facing direction is the direction from the character to the cursor world position
  186.             motor.facingDirection = (cursorWorldPosition - character.position);
  187.             motor.facingDirection.y = 0;           
  188.            
  189.             // Draw the cursor nicely
  190.             HandleCursorAlignment (cursorWorldPosition);
  191.            
  192.         #endif
  193.        
  194.     #endif
  195.        
  196.     // HANDLE CAMERA POSITION
  197.        
  198.     // Set the target position of the camera to point at the focus point
  199.     var cameraTargetPosition : Vector3 = character.position + initOffsetToPlayer + cameraAdjustmentVector * cameraPreview;
  200.    
  201.     // Apply some smoothing to the camera movement
  202.     mainCameraTransform.position = Vector3.SmoothDamp (mainCameraTransform.position, cameraTargetPosition, cameraVelocity, cameraSmoothing);
  203.    
  204.     // Save camera offset so we can use it in the next frame
  205.     cameraOffset = mainCameraTransform.position - character.position;
  206. }
  207.  
  208. public static function PlaneRayIntersection (plane : Plane, ray : Ray) : Vector3 {
  209.     var dist : float;
  210.     plane.Raycast (ray, dist);
  211.     return ray.GetPoint (dist);
  212. }
  213.  
  214. public static function ScreenPointToWorldPointOnPlane (screenPoint : Vector3, plane : Plane, camera : Camera) : Vector3 {
  215.     // Set up a ray corresponding to the screen position
  216.     var ray : Ray = camera.ScreenPointToRay (screenPoint);
  217.    
  218.     // Find out where the ray intersects with the plane
  219.     return PlaneRayIntersection (plane, ray);
  220. }
  221.  
  222. function HandleCursorAlignment (cursorWorldPosition : Vector3) {
  223.     if (!cursorObject)
  224.         return;
  225.    
  226.     // HANDLE CURSOR POSITION
  227.    
  228.     // Set the position of the cursor object
  229.     cursorObject.position = cursorWorldPosition;
  230.    
  231.     // Hide mouse cursor when within screen area, since we're showing game cursor instead
  232.     Screen.showCursor = (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height);
  233.    
  234.    
  235.     // HANDLE CURSOR ROTATION
  236.    
  237.     var cursorWorldRotation : Quaternion = cursorObject.rotation;
  238.     if (motor.facingDirection != Vector3.zero)
  239.         cursorWorldRotation = Quaternion.LookRotation (motor.facingDirection);
  240.    
  241.     // Calculate cursor billboard rotation
  242.     var cursorScreenspaceDirection : Vector3 = Input.mousePosition - mainCamera.WorldToScreenPoint (transform.position + character.up * cursorPlaneHeight);
  243.     cursorScreenspaceDirection.z = 0;
  244.     var cursorBillboardRotation : Quaternion = mainCameraTransform.rotation * Quaternion.LookRotation (cursorScreenspaceDirection, -Vector3.forward);
  245.    
  246.     // Set cursor rotation
  247.     cursorObject.rotation = Quaternion.Slerp (cursorWorldRotation, cursorBillboardRotation, cursorFacingCamera);
  248.    
  249.    
  250.     // HANDLE CURSOR SCALING
  251.    
  252.     // The cursor is placed in the world so it gets smaller with perspective.
  253.     // Scale it by the inverse of the distance to the camera plane to compensate for that.
  254.     var compensatedScale : float = 0.1 * Vector3.Dot (cursorWorldPosition - mainCameraTransform.position, mainCameraTransform.forward);
  255.    
  256.     // Make the cursor smaller when close to character
  257.     var cursorScaleMultiplier : float = Mathf.Lerp (0.7, 1.0, Mathf.InverseLerp (0.5, 4.0, motor.facingDirection.magnitude));
  258.    
  259.     // Set the scale of the cursor
  260.     cursorObject.localScale = Vector3.one * Mathf.Lerp (compensatedScale, 1, cursorSmallerWithDistance) * cursorScaleMultiplier;
  261.    
  262.     // DEBUG - REMOVE LATER
  263.     if (Input.GetKey(KeyCode.O)) cursorFacingCamera += Time.deltaTime * 0.5;
  264.     if (Input.GetKey(KeyCode.P)) cursorFacingCamera -= Time.deltaTime * 0.5;
  265.     cursorFacingCamera = Mathf.Clamp01(cursorFacingCamera);
  266.    
  267.     if (Input.GetKey(KeyCode.K)) cursorSmallerWithDistance += Time.deltaTime * 0.5;
  268.     if (Input.GetKey(KeyCode.L)) cursorSmallerWithDistance -= Time.deltaTime * 0.5;
  269.     cursorSmallerWithDistance = Mathf.Clamp01(cursorSmallerWithDistance);
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement