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 GoatSpriteObj;
- private SpriteRenderer GoatSprite;
- public GameObject GoatTrails;
- private ParticleSystem.EmissionModule GoatTrailsParticlesModule;
- private ParticleSystem GoatTrailsParticles;
- private Rigidbody2D GoatRb;
- public GameObject AncorObjPrefab;
- private Vector2 MouseForce;
- private GameObject ShootStartPoint;
- private GameObject ShootEndPoint;
- private Vector3 MousePos;
- private Vector3 ShootForce;
- public float ShootForceAmnt = 100f;
- public float MaxShootSpeed = 10;
- //private float MaxShootSpeedSave;
- private OnGround OnTheGround;
- public Animator GoatAnimator;
- private bool FacingRight;
- private float AngleInRadians;
- private Vector2 OnGroundNormal;
- public GameObject TakeOffParticles;
- public LayerMask SceneCollisionLayer;
- public bool DoRayCast = true;
- public float RayLength = 1;
- private Vector3 JumpForce;
- public void Fliper(float horizontal)
- {
- if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
- {
- FacingRight = !FacingRight;
- Vector3 theScale = GoatObj.transform.position;
- theScale.x *= -1;
- GoatObj.transform.localScale = theScale;
- }
- }
- 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>();
- OnTheGround = GoatObj.GetComponent<OnGround>();
- GoatSprite = GoatSpriteObj.GetComponent<SpriteRenderer>();
- GoatTrailsParticles = GoatTrails.GetComponent<ParticleSystem>();
- GoatTrailsParticlesModule = GoatTrailsParticles.emission; //= GoatTrailsParticles.GetComponent<ParticleSystem.EmissionModule>();
- //MaxShootSpeedSave = MaxShootSpeed;
- }
- void Update()
- {
- Vector2 MousePos = MousePosition();
- Vector3 GameObjPos = GoatObj.transform.position;
- Vector3 ShootForce = Vector3.zero;
- JumpForce = 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
- {
- JumpForce = ShootStartPoint.transform.position - ShootEndPoint.transform.position;
- float ShootMag = JumpForce.magnitude;// * ShootForceAmnt;
- JumpForce.Normalize();
- Vector3 ShootForceMax = Vector3.ClampMagnitude(JumpForce * (ShootMag * ShootForceAmnt), MaxShootSpeed);
- ShootForce = GameObjPos + ShootForceMax;
- Destroy(ShootStartPoint);
- Destroy(ShootEndPoint);
- if (OnTheGround.IsOnGround)
- {
- var TakeOffExplosion = (GameObject)Instantiate(
- TakeOffParticles,
- GameObjPos,
- GoatObj.transform.rotation);
- Destroy(TakeOffExplosion, 1.0f);
- }
- //OnTheGround.IsOnGround = false;
- }
- if (DoRayCast)
- {
- List<Vector3> RayList = new List<Vector3>();
- Vector3 dirDown = Vector3.down;
- RayList.Add(dirDown * RayLength);
- Vector3 dirForward = Vector3.right + Vector3.down;
- RayList.Add(dirForward.normalized * RayLength);
- Vector3 dirBack = Vector3.left + Vector3.down;
- RayList.Add(dirBack.normalized * RayLength);
- RayList.Add(Vector3.left * RayLength);
- RayList.Add(Vector3.right * RayLength);
- for (int i = 0; i < RayList.Count; i++)
- {
- RaycastHit2D hit = Physics2D.Raycast(GoatFeet.transform.position, RayList[i], 1 * RayLength, SceneCollisionLayer);
- Debug.DrawLine(GoatFeet.transform.position, GoatFeet.transform.position + RayList[i], Color.blue);
- if (hit.collider != null)
- {
- //Debug.DrawLine(GoatFeet.transform.position, new Vector2(GoatFeet.transform.position.x, GoatFeet.transform.position.y) + dirTest * 0.3f, Color.red);
- Debug.DrawLine(GoatFeet.transform.position, hit.point, Color.red);
- OnTheGround.IsOnGround = true;
- }
- }
- }
- if (OnTheGround.IsOnGround)
- {
- GoatAnimator.SetBool("Jump", false);
- GoatRb.AddForce(ShootForce);
- float AngleInRadians = Mathf.Atan2(JumpForce.y, JumpForce.x);
- AngleInRadians *= -1;
- OnGroundNormal = OnTheGround.GroundNormal;
- float AngleInRadians2 = Mathf.Atan2(OnGroundNormal.y, OnGroundNormal.x);
- float AngleInDegrees2 = AngleInRadians2 * Mathf.Rad2Deg;
- GoatSprite.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees2 - 90);
- GoatTrailsParticlesModule.enabled = false;
- }
- else
- {
- GoatAnimator.SetBool("Jump", true);
- GoatTrailsParticlesModule.enabled = true;
- if (GoatRb.velocity.x >= .1)
- {
- GoatSprite.flipX = false;
- }
- if (GoatRb.velocity.x <= .1)
- {
- GoatSprite.flipX = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment