cwisbg

GoatCtlr_v1

Oct 2nd, 2018
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GoatCtrl_v1 : MonoBehaviour {
  6.    
  7.     public GameObject GoatObj;
  8.     public GameObject GoatFeet;
  9.     public GameObject HitPointObj;
  10.     private Rigidbody2D GoatRb;
  11.     private Collider2D GoatCollider;
  12.     public GameObject AimTargetObj;
  13.     private Vector2 AimTargetPos;
  14.     public GameObject AncorObjPrefab;
  15.     private Vector2 MouseForce;
  16.     private GameObject ShootStartPoint;
  17.     private GameObject ShootEndPoint;
  18.     private Vector3 MousePos;
  19.     private Vector3 ShootForce;
  20.     private Vector3 GoatDirection;
  21.     private Vector3 lastPosition;
  22.     public float speed = 5f;
  23.     public float ShootForceAmnt = 100f;
  24.     public float MaxShootSpeed = 10;
  25.     private OnGround OnTheGround;
  26.  
  27.     Vector3 MousePosition()
  28.     {
  29.         Vector3 MousePos = Input.mousePosition;
  30.         MousePos = Camera.main.ScreenToWorldPoint(MousePos);
  31.         MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
  32.         return MousePos;
  33.     }
  34.     void Start () {
  35.         GoatRb = GoatObj.GetComponent<Rigidbody2D>();
  36.         GoatCollider = GoatObj.GetComponent<Collider2D>();
  37.         OnTheGround = GoatObj.GetComponent<OnGround>();
  38.     }
  39.     void Update() {
  40.         Vector2 MousePos = MousePosition();
  41.         Vector3 GameObjPos = GoatObj.transform.position;
  42.         Vector3 ShootForce = Vector3.zero;
  43.         if (Input.GetMouseButtonDown(0))// Create start and end point targets on mouse click
  44.         {
  45.             if (ShootStartPoint == null) // if start point not created yet, create
  46.             {
  47.                 var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  48.                 ShootStartPoint = HitPointObj;
  49.             }
  50.             if (ShootEndPoint == null) // if end point not created yet, create
  51.             {
  52.                 var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
  53.                 ShootEndPoint = HitPointObj;
  54.             }
  55.         }
  56.         if (Input.GetMouseButton(0))// Update End position target while mouse is down
  57.         {
  58.             if (ShootEndPoint != null)
  59.             {
  60.                 ShootEndPoint.transform.position = MousePos;
  61.             }
  62.             Debug.DrawLine(ShootStartPoint.transform.position, ShootEndPoint.transform.position);
  63.         }
  64.         if (Input.GetMouseButtonUp(0))// Destroy Targets, clean up icons, shoot goat
  65.         {
  66.             Vector3 JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
  67.             float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
  68.             JumpForce.Normalize();
  69.             Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * (ShootMag* ShootForceAmnt), MaxShootSpeed);
  70.             ShootForce =  ShootForceMax - GameObjPos;
  71.             //Debug.Log(ShootForce);
  72.             Destroy(ShootStartPoint);
  73.             Destroy(ShootEndPoint);
  74.         }
  75.          if (OnTheGround.IsOnGround)
  76.         {
  77.             GoatRb.AddForce(ShootForce);
  78.         }
  79.         lastPosition = GoatObj.transform.position;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment