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;
- public Camera RayCam;
- 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;
- public float SearchCollisionDistance = 5f;
- public LayerMask SceneCollisionLayer;
- private OnGround OnTheGround;
- Vector2 RaySearcher(GameObject Goat)
- {
- Vector2 rayForce = Vector2.zero;
- RaycastHit2D hit = Physics2D.Raycast(Goat.transform.position, Goat.transform.up, SearchCollisionDistance, SceneCollisionLayer);
- Debug.DrawLine(Goat.transform.position, Goat.transform.position-Goat.transform.up, Color.red);
- if (hit.collider != null)
- {
- rayForce = hit.normal;
- Debug.DrawLine(hit.point, hit.point + hit.normal, Color.red);
- }
- return rayForce;
- }
- 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>();
- //Debug.Log(GoatCollider);
- }
- void OnCollisionEnter2D(Collision2D GoatCollider)
- {
- //Debug.Log(GoatCollider.transform);
- if (GoatCollider.gameObject.name == "Ground")
- {
- Destroy(GoatCollider.gameObject);
- }
- }
- // Update is called once per frame
- void Update() {
- //GoatRb.AddForce(Vector2.right * speed);
- 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);
- }
- /*
- GoatDirection = (GameObjPos - lastPosition);
- //GoatDirection.Normalize();
- GoatDirection += GameObjPos;
- Vector2 GoatPos = GoatObj.transform.position;
- Vector2 AimTargetPos = GoatDirection;// AimTargetObj.transform.position;
- Vector2 diff = AimTargetPos - GoatPos;
- float AngleInRadians = Mathf.Atan2(diff.y, diff.x);
- float AngleInDegrees = AngleInRadians * Mathf.Rad2Deg ;
- GoatObj.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees);
- Vector2 dir = GoatRb.velocity;
- float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- GoatObj.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- Vector2 dirN = dir.normalized;
- //Vector2 RayHit = RaySearcher(GoatFeet);
- Quaternion CurrentDirection = Quaternion.Euler(GoatObj.transform.rotation.x, GoatObj.transform.rotation.y, GoatObj.transform.rotation.z);// Quaternion.identity;
- */
- if (Input.GetMouseButtonUp(0))// Destroy Targets, clean up icons, shoot goat
- {
- //GoatRb.simulated = true;
- Vector3 JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
- float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
- Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * ShootForceAmnt, MaxShootSpeed);
- ShootForce = ShootForceMax - GameObjPos;
- //Debug.Log(ShootForce);
- //ShootForce -= GameObjPos;
- Destroy(ShootStartPoint);
- Destroy(ShootEndPoint);
- }
- /*
- bool OnGround = false;
- Vector2 dirTest = Vector2.down;
- Vector2 RayHitPosition = Vector2.zero;
- RaycastHit2D hit = Physics2D.Raycast(GameObjPos, dirTest, 3f, SceneCollisionLayer);
- Debug.DrawLine(GoatFeet.transform.position, new Vector2(GoatFeet.transform.position.x, GoatFeet.transform.position.y) + dirTest, Color.yellow);
- if (hit.collider != null)
- {
- //float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
- //GoatObj.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- HitPointObj.transform.position = hit.point;
- //GoatRb.AddForce(GameObjPos - ShootForce);
- OnGround = true;
- // Rotation stuff
- //float angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg;
- //GoatObj.transform.rotation = Quaternion.Lerp(CurrentDirection, Quaternion.AngleAxis(angle, Vector3.forward), 10.0f*Time.deltaTime);
- //Debug.DrawLine(GoatFeet.transform.position, hit.point + hit.normal, Color.blue);
- }
- */
- //Debug.Log(OnTheGround.IsOnGround);
- if (OnTheGround.IsOnGround)
- {
- GoatRb.AddForce(ShootForce);
- }
- lastPosition = GoatObj.transform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment