C# Unity Expression


SUBMITTED BY: Guest

DATE: Sept. 27, 2014, 8:37 p.m.

FORMAT: Text only

SIZE: 1.6 kB

HITS: 441194

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Movement : MonoBehaviour
  4. {
  5. private Vector3 targetPosition1;
  6. public int speed = 60;
  7. Plane playerPlane;
  8. Ray ray;
  9. float hitdist;
  10. // Use this for initialization
  11. void Start ()
  12. {
  13. }
  14. // Update is called once per frame
  15. void Update ()
  16. {
  17. if (Input.GetMouseButtonDown(1))
  18. {
  19. playerPlane = new Plane(Vector3.up, transform.position);
  20. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  21. hitdist = 0.0f;
  22. if (playerPlane.Raycast(ray, hitdist))
  23. {
  24. Vector3 targetPoint = ray.GetPoint(hitdist);
  25. Vector3 targetPosition = ray.GetPoint(hitdist);
  26. Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
  27. transform.rotation = targetRotation;
  28. }
  29. Vector3 dir = targetPosition1 - transform.position;
  30. float dist = dir.magnitude;
  31. float move = speed * Time.deltaTime;
  32. if(dist > move)
  33. {
  34. transform.position += dir.normalized * move;
  35. }
  36. else
  37. {
  38. transform.position = targetPosition1;
  39. }
  40. transform.position += (targetPosition1 - transform.position).normalized * speed * Time.deltaTime;
  41. }
  42. }

comments powered by Disqus