Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour {
- //Public Vars
- [SerializeField]
- public Rigidbody2D arrowpoint;
- [SerializeField]
- public Camera camera;
- [SerializeField]
- public Rigidbody2D arrow;
- public static bool shoot;
- public Vector3 mousePosition;
- public PlayerController play;
- //Private Vars
- private Vector3 direction;
- private float distanceFromObject;
- private float angle;
- private Rigidbody2D rigidbody;
- private bool isAiming;
- private bool rotating;
- private Animator anim;
- public Vector3 dir;
- public bool test;
- void Start() {
- rigidbody = GetComponent<Rigidbody2D> ();
- anim = GetComponent<Animator> ();
- shoot = false;
- test = true;
- rotating = false;
- }
- void FixedUpdate() {
- HandleRotation ();
- }
- void Update(){
- HandleShoot();
- AddPower ();
- HandleController ();
- ArrowFlight();
- }
- void HandleRotation(){
- if (Input.GetMouseButton (0)) {
- if (ArrowControl.thrust < 1000f) {
- ArrowControl.thrust++;
- ArrowControl.thrust++;
- } else {
- ArrowControl.thrust = Random.Range (900f, 1000f);
- }
- isAiming = true;
- Vector3 pos = Camera.main.WorldToScreenPoint (transform.position);
- Vector3 dir = Input.mousePosition - pos;
- angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
- transform.rotation = Quaternion.AngleAxis (Mathf.Clamp(angle, -6, 53), Vector3.forward);
- if (test) {
- Instantiate (arrow, arrowpoint.position, Quaternion.AngleAxis (angle, Vector3.forward));
- test = false;
- }
- if (ArrowControl.arrow_instantiated)
- ArrowControl.arrowrb.transform.position = arrowpoint.position;
- }
- }
- void HandleShoot()
- {
- if (isAiming) {
- anim.SetBool("Aiming", true);
- }
- if (Input.GetMouseButtonUp (0)) {
- shoot = true;
- isAiming = false;
- anim.SetBool ("Aiming", false);
- }
- }
- void AddPower(){
- if (shoot) {
- ArrowControl.arrow_instantiated = false;
- rotating = true;
- Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
- Vector3 dir = Input.mousePosition - pos;
- angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- ArrowControl.arrowrb.AddForce(dir.normalized * ArrowControl.thrust);
- ArrowControl.arrowrb.gravityScale = 1f;
- test = true;
- }
- }
- void HandleController(){
- shoot = false;
- }
- void ArrowFlight(){
- float t = ArrowControl.thrust * Mathf.Sin (angle*Mathf.Deg2Rad);
- Quaternion temp = ArrowControl.arrowrb.transform.rotation;
- if (temp.z > angle && temp.z < 360 - angle) {
- temp.z -= 4 * angle * Time.deltaTime / t;
- } else {
- temp.z -= 2 * angle * Mathf.Sin (Mathf.PI / 2) * Mathf.Sin (Mathf.PI * Time.deltaTime / (2 * t));
- }
- ArrowControl.arrowrb.transform.rotation = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement