Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CharacterController : MonoBehaviour
- {
- public FixedJoystick RunAxes;
- public FixedJoystick CameraAxes;
- public Vector3 movement;
- public GameObject Cam;
- public GameObject AllFood;
- public Camera OurCamera;
- public Rigidbody rb;
- public Material FoodSelect;
- public Material Standart;
- public float Leght=10;
- public float Ver;
- public float Hor;
- public float speed;
- public float sensitive=3f;
- public float minX = -360;
- public float maxX = 360;
- public float minY = -60;
- public float maxY = 60;
- public float rotX;
- public float rotY;
- public float height;
- Quaternion originalRot;
- public Quaternion XQuaternion;
- public Quaternion YQuaternion;
- void Start()
- {
- rb.freezeRotation=true;
- originalRot = transform.rotation;
- }
- void Update()
- {
- //rotX += CrossPlatformInputManager.GetAxis("Mouse X") * sensitive;
- //rotY += CrossPlatformInputManager.GetAxis("Mouse Y") * sensitive;
- rotX = rotX % 360;
- rotY = rotY % 360;
- rotX = Mathf.Clamp(rotX, minX, maxX);
- rotY = Mathf.Clamp(rotY, minY, maxY);
- XQuaternion = Quaternion.AngleAxis(rotX, Vector3.up);
- YQuaternion = Quaternion.AngleAxis(rotY, Vector3.left);
- transform.rotation = originalRot * XQuaternion;
- Cam.transform.localRotation = originalRot * transform.rotation * YQuaternion;
- Vector3 lineOrigin = OurCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
- Vector3 EndLine = OurCamera.transform.TransformDirection(Vector3.forward) * Leght;
- Debug.DrawLine(lineOrigin, EndLine, Color.red);
- Ray R = new Ray(lineOrigin, EndLine);
- RaycastHit hit = new RaycastHit();
- for (int i = 0; i < AllFood.transform.childCount; i++)
- {
- AllFood.transform.GetChild(i).GetComponent<Renderer>().sharedMaterial = Standart;
- }
- if (Physics.Raycast(R, out hit, Leght))
- {
- if (hit.collider.gameObject.tag == "Food")
- {
- hit.collider.gameObject.GetComponent<Renderer>().sharedMaterial = FoodSelect;
- if (Input.GetMouseButtonDown(0))
- {
- Collider[] _col = Physics.OverlapSphere(hit.collider.gameObject.transform.position, 2);
- foreach (var NearByFood in _col)
- {
- Rigidbody rb = NearByFood.GetComponent<Rigidbody>();
- if (rb != null)
- {
- rb.AddExplosionForce(300, hit.collider.gameObject.transform.position, 2);
- hit.collider.gameObject.SetActive(false);
- }
- }
- }
- }
- }
- }
- void FixedUpdate()
- {
- Vector3 direction = Cam.transform.forward * RunAxes.Vertical + Cam.transform.right * RunAxes.Horizontal;
- rb.velocity = direction * speed * Time.fixedDeltaTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement