Unity3d中最简单物体的旋转移动

  1. void Awake (){  

  2. }      

  3. //初始化函数,在游戏开始时系统自动调用。一般用来创建变量之类的东西。  

  4.   

  5. void Start(){  

  6. }  

  7. //初始化函数,在所有Awake函数运行完之后(一般是这样,但不一定),在所有Update函数前系统自动条用。一般用来给变量赋值。

  8. 我们通常书写的脚本,并不会定义[ExecuteInEditMode]这个Attribute,所以Awake和Start都只有在Runtime中才会执行,文章出处【狗刨学习网】。



  9. using UnityEngine;
  10. using System.Collections;



  11. public class square1 : MonoBehaviour {
  12. public float m_speed = 1;
  13. protected Transform m_transform;
  14. //是否被拖拽
  15. private bool onDrag=false;
  16. //旋转速度
  17. public float speed=6f;
  18. //阻尼速度
  19. private float tempSpeed;
  20. //水平
  21. private float axisX;
  22. //竖直
  23. private float axisY;
  24. //鼠标移动的距离
  25. private float cXY;



  26. // Use this for initialization
  27.         void Start () {
  28. //调用
  29. m_transform = this.transform;

  30.         }

  31.         // Update is called once per frame
  32.         void Update () {



  33. float movev = 0;
  34. float moveh = 0;
  35. if (Input.GetKey(KeyCode.UpArrow))
  36. {
  37. movev += m_speed * Time.deltaTime;
  38. }



  39. if (Input.GetKey(KeyCode.DownArrow))
  40. {
  41. movev -= m_speed * Time.deltaTime;
  42. }



  43. if (Input.GetKey(KeyCode.RightArrow))
  44. {
  45. moveh += m_speed * Time.deltaTime;
  46. }



  47. if (Input.GetKey(KeyCode.LeftArrow))
  48. {
  49. moveh -= m_speed * Time.deltaTime;
  50. }



  51. gameObject.transform.Rotate(new Vector3(axisX,axisY,0)*Rigid(),Space.World);
  52. if (!Input.GetMouseButton(0))
  53. {
  54. onDrag = false;
  55. }
  56. transform.Translate(Vector3.forward*Time.captureFramerate);
  57. this.m_transform.Translate(new Vector3(moveh, 0, movev));
  58.         }

  59. //接受鼠标按下
  60. void OnMouseDown()
  61. {
  62. axisX = 0f;
  63. axisY = 0f;
  64. }



  65. //鼠标拖拽时的操作
  66. void OnMouseDrag()
  67. {
  68. onDrag = true;
  69. axisX = -Input.GetAxis("Mouse X");
  70. axisY = Input.GetAxis("Mouse Y");
  71. cXY = Mathf.Sqrt(axisX*axisX+axisY*axisY);
  72. if (cXY==0f)
  73. {
  74. cXY = 1f;
  75. }
  76. }
  77. //计算阻尼
  78. float Rigid()
  79. {
  80. if (onDrag)
  81. {
  82. tempSpeed = speed;
  83. }
  84. else
  85. {
  86. if (tempSpeed>0)
  87. {
  88. tempSpeed -= speed * 2 * Time.deltaTime / cXY;
  89. }
  90. else
  91. {
  92. tempSpeed = 0;
  93. }
  94. }
  95. return tempSpeed;
  96. }
  97. //实现鼠标的拖拽移动
  98. IEnumerator OnMouse()
  99. {
  100. var camear = Camera.mainCamera;
  101. if (camear)
  102. {
  103. Vector3 screenPosition = camear.WorldToScreenPoint(transform.position);
  104. Vector3 mscreenPosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPosition.z);
  105. Vector3 offset = transform.position - camear.ScreenToWorldPoint(mscreenPosition);
  106. print("drag strating:"+transform.name);



  107. while (Input.GetMouseButton(0))
  108. {
  109. mscreenPosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPosition.z);
  110. transform.position = offset + camear.ScreenToWorldPoint(mscreenPosition);
  111. yield return new WaitForEndOfFrame();
  112. }
  113. print("drag compeleted");
  114. }
  115. }
  116. }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值