Unity3d Learning Tutorial C # Rotating Lens

Source: Internet
Author: User

if (Input.getkey (KEYCODE.Q))
{
Transform. Rotate (0,-50*time.deltatime,0,space.self);
}
if (Input.getkey (KEYCODE.E))
{
Transform. Rotate (0,50*time.deltatime,0,space.self);
}

Mimic WOW lens operation

  1. Using Unityengine;
  2. Using System.Collections;
  3. ///
  4. Attaching this script to any lens allows it to have a WOW lens control
  5. ///
  6. public class Wowcamera:monobehaviour
  7. {
  8. ///
  9. Lens target
  10. ///
  11. Public Transform Target;
  12. ///
  13. The distance of the lens from the target
  14. ///
  15. public float Distance = 30.0f;
  16. ///
  17. Maximum Lens distance
  18. ///
  19. public float maxdistance = 30.0f;
  20. ///
  21. The mouse wheel pulls the far speed coefficient
  22. ///
  23. public float scrollfactor = 10.0f;
  24. ///
  25. Lens Rotation speed ratio
  26. ///
  27. public float rotatefactor = 10.0f;
  28. ///
  29. Lens horizontal Surround Angle
  30. ///
  31. public float horizontalangle = 45;
  32. ///
  33. Lens vertical Surround Angle
  34. ///
  35. public float verticalangle = 0;
  36. Private Transform Mcameratransform;
  37. void Start ()
  38. {
  39. Mcameratransform = transform;
  40. }
  41. void Update ()
  42. {
  43. Wheel forward: close the distance; Roller back: Pull Away
  44. var scrollamount = Input.getaxis (Gamesetting.mousescrollwheel);
  45. Distance-= ScrollAmount * Scrollfactor;
  46. Ensure lens distance is legal
  47. if (Distance < 0)
  48. Distance = 0;
  49. else if (Distance > MaxDistance)
  50. Distance = maxdistance;
  51. Press and hold the mouse button to move, the lens rotates with it
  52. var ismouseleftbuttondown = Input.getmousebutton (0);
  53. var ismouserightbuttondown = Input.getmousebutton (1);
  54. if (Ismouseleftbuttondown | | ismouserightbuttondown)
  55. {
  56. Screen.lockcursor = true;
  57. var AxisX = Input.getaxis (gamesetting.mousex);
  58. var axisy = Input.getaxis (Gamesetting.mousey);
  59. Horizontalangle + = AxisX * Rotatefactor;
  60. Verticalangle + = Axisy * Rotatefactor;
  61. if (Ismouserightbuttondown)
  62. {
  63. If the right mouse button is moved, the rotated character is aligned on the horizontal plane with the lens orientation
  64. target.rotation = Quaternion.euler (0, Horizontalangle, 0);
  65. }
  66. }
  67. Else
  68. {
  69. Screen.lockcursor = false;
  70. }
  71. Adjust position and orientation by lens distance
  72. var rotation = Quaternion.euler (-verticalangle, Horizontalangle, 0);
  73. var offset = rotation * Vector3.back * Distance;
  74. Mcameratransform.position = target.position + offset;
  75. Mcameratransform.rotation = rotation;
  76. }
  77. }

Unity3d Learning Tutorial C # Rotating Lens

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.