Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GoatCtrl_v1 : MonoBehaviour {
- public GameObject GoatObj;
- public GameObject GoatFeet;
- public GameObject HitPointObj;
- private Rigidbody2D GoatRb;
- private Collider2D GoatCollider;
- public GameObject AimTargetObj;
- private Vector2 AimTargetPos;
- public GameObject AncorObjPrefab;
- private Vector2 MouseForce;
- private GameObject ShootStartPoint;
- private GameObject ShootEndPoint;
- private Vector3 MousePos;
- private Vector3 ShootForce;
- private Vector3 GoatDirection;
- private Vector3 lastPosition;
- public float speed = 5f;
- public float ShootForceAmnt = 100f;
- public float MaxShootSpeed = 10;
- private OnGround OnTheGround;
- Vector3 MousePosition()
- {
- Vector3 MousePos = Input.mousePosition;
- MousePos = Camera.main.ScreenToWorldPoint(MousePos);
- MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
- return MousePos;
- }
- void Start () {
- GoatRb = GoatObj.GetComponent<Rigidbody2D>();
- GoatCollider = GoatObj.GetComponent<Collider2D>();
- OnTheGround = GoatObj.GetComponent<OnGround>();
- }
- void Update() {
- Vector2 MousePos = MousePosition();
- Vector3 GameObjPos = GoatObj.transform.position;
- Vector3 ShootForce = Vector3.zero;
- if (Input.GetMouseButtonDown(0))// Create start and end point targets on mouse click
- {
- if (ShootStartPoint == null) // if start point not created yet, create
- {
- var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
- ShootStartPoint = HitPointObj;
- }
- if (ShootEndPoint == null) // if end point not created yet, create
- {
- var HitPointObj = (GameObject)Instantiate(AncorObjPrefab, MousePos, Quaternion.identity);
- ShootEndPoint = HitPointObj;
- }
- }
- if (Input.GetMouseButton(0))// Update End position target while mouse is down
- {
- if (ShootEndPoint != null)
- {
- ShootEndPoint.transform.position = MousePos;
- }
- Debug.DrawLine(ShootStartPoint.transform.position, ShootEndPoint.transform.position);
- }
- if (Input.GetMouseButtonUp(0))// Destroy Targets, clean up icons, shoot goat
- {
- Vector3 JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
- float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
- JumpForce.Normalize();
- Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * (ShootMag* ShootForceAmnt), MaxShootSpeed);
- ShootForce = ShootForceMax - GameObjPos;
- //Debug.Log(ShootForce);
- Destroy(ShootStartPoint);
- Destroy(ShootEndPoint);
- }
- if (OnTheGround.IsOnGround)
- {
- GoatRb.AddForce(ShootForce);
- }
- lastPosition = GoatObj.transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment