Guest User

Untitled

a guest
Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // gives us vector to direction of target
  2. Vector3 inverseVect = Input.mousePosition;
  3. // calculate angle by which you have to rotate
  4. // Note -: This angle is calculated every Frame of FixedUpdate
  5. float rotationAngle = Mathf.Atan2(inverseVect.x, inverseVect.z) * Mathf.Rad2Deg;
  6. // Now calculate rotationVelocity to be applied every frame
  7. Vector3 rotationVelocity = (Vector3.up * rotationAngle) * rotationSpeed * Time.deltaTime;
  8. // Calaculate his delta velocity i.e required - current
  9. Vector3 deltavel = (rotationVelocity - rb.angularVelocity);
  10. // Apply the force to rotate
  11. rb.AddTorque(deltavel, ForceMode.Impulse);
  12.  
  13. //поворот физикой за мышкой в пустом пространстве
  14. //(если смотреть на мышь)get right position of mouse if the camera is Perspective
  15. //подробнее goo.gl/Fc6icj
  16. Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  17. float midPoint = (transform.position - Camera.main.transform.position).magnitude * 0.5f;
  18. Vector3 targetPosition = (mouseRay.origin + mouseRay.direction * midPoint);
  19. //get position of target object(если смотреть на объект)
  20. //Vector3 targetPosition = targetObject.transform.position;
  21. //gives us vector to direction of target
  22. Vector3 inverseVect = transform.InverseTransformPoint(targetPosition);
  23. //calculate angle by which you have to rotate.Note: this angle is calculated every Frame of FixedUpdate
  24. float rotationAngle = Mathf.Atan2(inverseVect.x, inverseVect.z) * Mathf.Rad2Deg;
  25. //Now calculate rotationVelocity to be applied every frame
  26. Vector3 rotationVelocity = (Vector3.up * rotationAngle) * rotationSpeed * Time.deltaTime;
  27. //Calaculate his delta velocity i.e required - current
  28. Vector3 deltavel = (rotationVelocity - rb.angularVelocity);
  29. //Apply the force to rotate
  30. rb.AddTorque(deltavel, ForceMode.Impulse);
  31. //служ. инфо
  32. Debug.DrawLine(transform.position, targetPosition, Color.magenta);
  33. print("targetPosition: " + targetPosition +
  34. " targetObject.transform.position: "+ targetObject.transform.position +
  35. " transform.position: "+ transform.position);
Add Comment
Please, Sign In to add comment