SHARE
TWEET

Untitled

a guest Oct 21st, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class CharacterController : MonoBehaviour
  7. {
  8.     public FixedJoystick RunAxes;
  9.     public FixedJoystick CameraAxes;
  10.  
  11.     public Vector3 movement;
  12.  
  13.     public GameObject Cam;
  14.     public GameObject AllFood;
  15.  
  16.     public Camera OurCamera;
  17.  
  18.     public Rigidbody rb;
  19.  
  20.     public Material FoodSelect;
  21.     public Material Standart;
  22.  
  23.     public float Leght=10;
  24.     public float Ver;
  25.     public float Hor;
  26.     public float speed;
  27.     public float sensitive=3f;
  28.     public float minX = -360;
  29.     public float maxX = 360;
  30.     public float minY = -60;
  31.     public float maxY = 60;
  32.     public float rotX;
  33.     public float rotY;
  34.     public float height;
  35.  
  36.     Quaternion originalRot;
  37.     public Quaternion XQuaternion;
  38.     public Quaternion YQuaternion;
  39.  
  40.     void Start()
  41.     {
  42.         rb.freezeRotation=true;
  43.         originalRot = transform.rotation;  
  44.     }
  45.  
  46.     void Update()
  47.     {
  48.        
  49.  
  50.         //rotX += CrossPlatformInputManager.GetAxis("Mouse X") * sensitive;
  51.         //rotY += CrossPlatformInputManager.GetAxis("Mouse Y") * sensitive;
  52.  
  53.         rotX = rotX % 360;
  54.         rotY = rotY % 360;
  55.  
  56.         rotX = Mathf.Clamp(rotX, minX, maxX);
  57.         rotY = Mathf.Clamp(rotY, minY, maxY);
  58.  
  59.          XQuaternion = Quaternion.AngleAxis(rotX, Vector3.up);
  60.          YQuaternion = Quaternion.AngleAxis(rotY, Vector3.left);
  61.  
  62.         transform.rotation = originalRot * XQuaternion;
  63.         Cam.transform.localRotation = originalRot * transform.rotation * YQuaternion;
  64.  
  65.        
  66.  
  67.  
  68.         Vector3 lineOrigin = OurCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
  69.  
  70.         Vector3 EndLine = OurCamera.transform.TransformDirection(Vector3.forward) * Leght;
  71.  
  72.         Debug.DrawLine(lineOrigin, EndLine, Color.red);
  73.  
  74.         Ray R = new Ray(lineOrigin, EndLine);
  75.  
  76.         RaycastHit hit = new RaycastHit();
  77.  
  78.         for (int i = 0; i < AllFood.transform.childCount; i++)
  79.         {
  80.             AllFood.transform.GetChild(i).GetComponent<Renderer>().sharedMaterial = Standart;
  81.  
  82.         }
  83.  
  84.         if (Physics.Raycast(R, out hit, Leght))
  85.         {
  86.             if (hit.collider.gameObject.tag == "Food")
  87.             {
  88.                 hit.collider.gameObject.GetComponent<Renderer>().sharedMaterial = FoodSelect;
  89.                 if (Input.GetMouseButtonDown(0))
  90.                 {
  91.                     Collider[] _col = Physics.OverlapSphere(hit.collider.gameObject.transform.position, 2);
  92.                     foreach (var NearByFood in _col)
  93.                     {
  94.                         Rigidbody rb = NearByFood.GetComponent<Rigidbody>();
  95.                         if (rb != null)
  96.                         {
  97.                             rb.AddExplosionForce(300, hit.collider.gameObject.transform.position, 2);
  98.                             hit.collider.gameObject.SetActive(false);
  99.                         }
  100.                     }
  101.                 }
  102.             }          
  103.         }
  104.     }
  105.  
  106.     void FixedUpdate()
  107.     {
  108.         Vector3 direction = Cam.transform.forward * RunAxes.Vertical + Cam.transform.right * RunAxes.Horizontal;
  109.         rb.velocity = direction * speed * Time.fixedDeltaTime;
  110.  
  111.     }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top