Posted By

joseques on 02/24/14


Tagged


Versions (?)

DnS.cs


 / Published in: C#
 

For UnityAnswers

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class DnS : MonoBehaviour {
  5. private bool clicked = false;
  6. private Vector3 originalPosition;
  7. private Quaternion originalRotation;
  8. public float maxDistance = 5;
  9. public float time = 5;
  10. public float rotationTimeMultiplier = 5;
  11. private bool notShooted = true;
  12. public float forceMultiplier = 500;
  13. public GameObject FlyierChar;
  14. public bool shooted = false;
  15. public GameObject instance;
  16. void Start(){
  17. rigidbody2D.gravityScale = 0;
  18. originalPosition = transform.position;
  19. originalRotation = transform.rotation;
  20. }
  21. void Update(){
  22. if(clicked){
  23. Vector3 mousePointerWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  24. Vector2 mousePos = new Vector2(mousePointerWorldPos.x, mousePointerWorldPos.y);
  25. transform.position = new Vector3(mousePos.x, mousePos.y, transform.position.z);
  26. float dst = Vector3.Distance(originalPosition, transform.position);
  27. if (dst > maxDistance){
  28. Vector3 vect = originalPosition - transform.position;
  29. vect = vect.normalized;
  30. vect *= (dst-maxDistance);
  31. transform.position += vect;
  32. }
  33. Quaternion newRotation = Quaternion.LookRotation(transform.position - originalPosition, Vector3.forward);
  34. newRotation.x = 0;
  35. newRotation.y = 0;
  36. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotationTimeMultiplier);
  37. } else {
  38. if(transform.position != originalPosition){
  39. Shoot(Vector3.Distance(transform.position, originalPosition)); //t as parameter to get the force
  40. }
  41. transform.position = Vector3.Lerp(transform.position, originalPosition, Time.deltaTime * time);
  42. transform.rotation = Quaternion.Lerp(transform.rotation,originalRotation,Time.deltaTime * time);
  43. }
  44. }
  45. void Shoot(float force){
  46. if(notShooted){
  47. instance = (GameObject)Instantiate(FlyierChar, transform.position, transform.rotation);
  48. instance.rigidbody2D.AddForce(transform.up * (force * forceMultiplier));
  49. shooted = true;
  50. //ShootManager.instance.followProjectile(instance.transform);
  51. Destroy(gameObject);
  52. notShooted = false;
  53. }
  54. }
  55. void OnMouseDown(){
  56. clicked = true;
  57. }
  58. void OnMouseUp(){
  59. clicked = false;
  60. }
  61. }

Report this snippet  

You need to login to post a comment.