Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Vector3 dir = new Vector3(0,0,0);
  2. float x2 = targetPos.x;
  3. float x1 = transform.position.x;
  4. float y2 = targetPos.y;
  5. float y1 = transform.position.y;
  6.  
  7. if(x2 - x1 >= 0)
  8. {
  9. if(Mathf.Abs(x2 - x1) < Mathf.Abs( (x2 - x1) - Screen.width )) dir.x = x2 - x1;
  10. else dir.x = (x2 - x1) - Screen.width;
  11. }
  12.  
  13. else
  14. {
  15. if(Mathf.Abs(x2 - x1) < Mathf.Abs( (x2 - x1) + Screen.width )) dir.x = x2 - x1;
  16. else dir.x = (x2 - x1) + Screen.width;
  17. }
  18.  
  19. if(y2 - y1 >= 0)
  20. {
  21. if(Mathf.Abs(y2 - y1) < Mathf.Abs( (y2 - y1) - Screen.height )) dir.y = y2 - y1;
  22. else dir.y = (y2 - y1) - Screen.height;
  23. }
  24.  
  25. else
  26. {
  27. if(Mathf.Abs(y2 - y1) < Mathf.Abs( (y2 - y1) + Screen.height )) dir.y = y2 - y1;
  28. else dir.y = (y2 - y1) + Screen.height;
  29. }
  30.  
  31. float rotZ = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
  32.  
  33. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,-rotZ), rotationSpeed);
  34. rb.AddForceAtPosition(transform.up * Thrust, transform.position - transform.up);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement