Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CharakterController : MonoBehaviour
  5. {
  6.     [SerializeField]
  7.     private float speed = 5.0f;
  8.  
  9.     [SerializeField]
  10.     private float rotateVel = 10.0f;
  11.  
  12.     private Vector3 worldMausePosition;
  13.     private Vector3 rotateMause;
  14.     private Camera mainCamera;
  15.     private Rigidbody charakterRigiBody;
  16.     private Collider coll;
  17.  
  18.     private Ray rayClick;
  19.  
  20.     void Start ()
  21.     {
  22.         mainCamera = FindObjectOfType<Camera>();
  23.         charakterRigiBody = GetComponent<Rigidbody>();
  24.         coll = GetComponent<Collider>();
  25.  
  26.         if(charakterRigiBody == null)
  27.         {
  28.             Debug.LogError("Componets dont Have rigibody componts");
  29.         }
  30.     }
  31.    
  32.     void FixedUpdate()
  33.     {
  34.         Run();
  35.         Rotation();
  36.     }
  37.  
  38.     void Update ()
  39.     {
  40.         //RunWitchRayCast();
  41.         GetInputMause();
  42.     }
  43.  
  44.     private void GetInputMause()
  45.     {
  46.         if (Input.GetMouseButtonDown(0))
  47.         {
  48.             //worldMausePosition = rotateMause = mainCamera.ViewportToWorldPoint(Input.mousePosition);
  49.             worldMausePosition = rotateMause = Camera.main.ScreenToWorldPoint();
  50.             Debug.Log("x: " + worldMausePosition.x + " y: " + worldMausePosition.y + " z: " + worldMausePosition.z);
  51.         }
  52.     }
  53.  
  54.     private void Run()
  55.     {
  56.         Vector3 vectorNormalize = new Vector3(worldMausePosition.normalized.x, worldMausePosition.normalized.y, worldMausePosition.normalized.z);
  57.         //Debug.Log("NormaX: " + vectorNormalize.x + " normaY: " + vectorNormalize.y);
  58.         if (vectorNormalize != Vector3.zero)
  59.         {
  60.             if (worldMausePosition != transform.position)
  61.             {
  62.                 charakterRigiBody.velocity = vectorNormalize * speed;
  63.             }
  64.         }
  65.     }
  66.  
  67.     private void RunWitchRayCast()
  68.     {
  69.         if (Input.GetMouseButton(0))
  70.         {
  71.             rayClick = Camera.main.ScreenPointToRay(Input.mousePosition);
  72.             RaycastHit ray;
  73.             Debug.Log("x: " + rayClick.direction.x + " y: " + rayClick.direction.y);
  74.             if (coll.Raycast(rayClick, out ray, 100.0F))
  75.             {
  76.                 transform.position = rayClick.GetPoint(100.0F);
  77.             }
  78.         }
  79.     }
  80.  
  81.     private void Rotation()
  82.     {
  83.         Vector2 dir = new Vector2(rotateMause.normalized.x, rotateMause.normalized.z);
  84.  
  85.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  86.         if (Mathf.Abs(angle) > 0.1f)
  87.         {
  88.             transform.transform.rotation = Quaternion.AngleAxis(-angle , -Vector3.up) ;
  89.             //rotateMause = Vector3.zero;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement