Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class CharakterController : MonoBehaviour
- {
- [SerializeField]
- private float speed = 5.0f;
- [SerializeField]
- private float rotateVel = 10.0f;
- private Vector3 worldMausePosition;
- private Vector3 rotateMause;
- private Camera mainCamera;
- private Rigidbody charakterRigiBody;
- private Collider coll;
- private Ray rayClick;
- void Start ()
- {
- mainCamera = FindObjectOfType<Camera>();
- charakterRigiBody = GetComponent<Rigidbody>();
- coll = GetComponent<Collider>();
- if(charakterRigiBody == null)
- {
- Debug.LogError("Componets dont Have rigibody componts");
- }
- }
- void FixedUpdate()
- {
- Run();
- Rotation();
- }
- void Update ()
- {
- //RunWitchRayCast();
- GetInputMause();
- }
- private void GetInputMause()
- {
- if (Input.GetMouseButtonDown(0))
- {
- //worldMausePosition = rotateMause = mainCamera.ViewportToWorldPoint(Input.mousePosition);
- worldMausePosition = rotateMause = Camera.main.ScreenToWorldPoint();
- Debug.Log("x: " + worldMausePosition.x + " y: " + worldMausePosition.y + " z: " + worldMausePosition.z);
- }
- }
- private void Run()
- {
- Vector3 vectorNormalize = new Vector3(worldMausePosition.normalized.x, worldMausePosition.normalized.y, worldMausePosition.normalized.z);
- //Debug.Log("NormaX: " + vectorNormalize.x + " normaY: " + vectorNormalize.y);
- if (vectorNormalize != Vector3.zero)
- {
- if (worldMausePosition != transform.position)
- {
- charakterRigiBody.velocity = vectorNormalize * speed;
- }
- }
- }
- private void RunWitchRayCast()
- {
- if (Input.GetMouseButton(0))
- {
- rayClick = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit ray;
- Debug.Log("x: " + rayClick.direction.x + " y: " + rayClick.direction.y);
- if (coll.Raycast(rayClick, out ray, 100.0F))
- {
- transform.position = rayClick.GetPoint(100.0F);
- }
- }
- }
- private void Rotation()
- {
- Vector2 dir = new Vector2(rotateMause.normalized.x, rotateMause.normalized.z);
- float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- if (Mathf.Abs(angle) > 0.1f)
- {
- transform.transform.rotation = Quaternion.AngleAxis(-angle , -Vector3.up) ;
- //rotateMause = Vector3.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement