Created
October 16, 2017 19:56
-
-
Save tklee1975/87bfe902cb28a94a473ef51cffbe651c to your computer and use it in GitHub Desktop.
Linked Prefab Sample Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class Tank : MonoBehaviour { | |
| // Link to Other Prefabs | |
| public GameObject bulletPrefab; | |
| // Constants | |
| const float kLaunchMin = 30; | |
| const float kLaunchMax = 100; | |
| const float kLaunchPowerMin = 10; | |
| const float kLaunchPowerMax = 30; | |
| // Internal Structure | |
| protected AimArrow mArrow; | |
| protected Bounds mTankTouchBounds; | |
| // | |
| protected Vector2 mPressPosition; | |
| protected bool mHasPressed; | |
| protected bool mMoveCancelled = false; | |
| protected float mPower; | |
| // Internal Structure | |
| protected Transform mTankTransform; | |
| protected Transform mTurretTransform; | |
| protected Transform mLaunchPositionTransform; | |
| protected Vector2 mTurretPosition; | |
| // Use this for initialization | |
| void Awake () { | |
| mTankTransform = transform.Find("TankEntity"); | |
| mTurretTransform = mTankTransform.Find("Turret"); | |
| mLaunchPositionTransform = mTurretTransform.Find("LaunchPosition"); | |
| // Define the mTankTouchBounds | |
| BoxCollider2D box = GetComponent<BoxCollider2D>(); | |
| mTankTouchBounds = box.bounds; | |
| // | |
| SetupAimArrow(); | |
| } | |
| protected Vector3 GetAimArrowTransformPosition() { | |
| return mTurretTransform.localPosition; | |
| } | |
| virtual protected void SetupAimArrow() | |
| { | |
| mArrow = GetComponentInChildren<AimArrow>(); | |
| mArrow.SetVisible(false); | |
| } | |
| void Start () { | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| UpdateStateWaitForInput(); | |
| } | |
| private bool IsTouchingMe(Vector3 touchPoint) { | |
| //Vector3 localPoint = touchPoint - | |
| Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchPoint); | |
| touchPosition.z = transform.position.z; | |
| Bounds bounds = mTankTouchBounds; | |
| bounds.center = transform.position; | |
| bool isTouching = bounds.Contains(touchPosition); | |
| Debug.Log("isTouching: touchPosition=" + touchPosition + " bound=" + bounds + " isTouching=" + isTouching); | |
| return isTouching; | |
| } | |
| public float GetLaunchPower(Vector2 mouseDiff) { | |
| float magnitude = mouseDiff.magnitude; | |
| // Get the value | |
| float diffRange = magnitude - kLaunchMin; | |
| float power; | |
| if(diffRange <= 0) { | |
| power = 0; | |
| } else { | |
| float scale = diffRange / (kLaunchMax - kLaunchMin); | |
| if(scale > 1) { | |
| scale = 1; | |
| } | |
| power = kLaunchPowerMin + (kLaunchPowerMax - kLaunchPowerMin) * scale; | |
| //power = kLaunchPowerMax; | |
| } | |
| Debug.Log("MouseDiff: magitude=" + magnitude + " power=" + power); | |
| return power; | |
| } | |
| public float GetTurretAngle() { | |
| float convertAngle = mTankTransform.localScale.x >= 0 ? 0 : 180; | |
| return mTurretTransform.eulerAngles.z + convertAngle; | |
| } | |
| /** | |
| * | | |
| * | | |
| * ------------ | |
| * | |
| */ | |
| public float GetAngle( Vector2 a, Vector2 b ) { | |
| var an = a.normalized; | |
| var bn = b.normalized; | |
| var x = a.x - b.x; | |
| var y = b.y - a.y; | |
| return Mathf.Atan2(-y, x) * Mathf.Rad2Deg; | |
| } | |
| float GetPowerScale(float power) { | |
| return power / kLaunchPowerMax; | |
| } | |
| void HandleRotateByMouse() | |
| { | |
| // Get Mouse Position | |
| Vector3 touchPosition = Input.mousePosition; | |
| Vector2 pos = new Vector2(touchPosition.x, touchPosition.y); | |
| // | |
| Vector2 diff = pos - mPressPosition; | |
| float launchPower = GetLaunchPower(diff); | |
| mArrow.SetArrowScale(GetPowerScale(launchPower)); | |
| mPower = launchPower * 10; | |
| mMoveCancelled = (launchPower <= 0) ? true : false; | |
| //float angle = Vector2.Angle(mPressPosition, pos); | |
| mArrow.Angle = GetAngle(mPressPosition, pos); | |
| SetTurretAngle(mArrow.Angle); | |
| } | |
| private Vector2 GetReferencePosition() { | |
| Vector3 turretPos = mTurretTransform.position; | |
| Vector3 screenPos = Camera.main.WorldToScreenPoint(turretPos); | |
| return new Vector2(screenPos.x, screenPos.y); | |
| } | |
| private void SetTurretAngle(float angle) | |
| { | |
| float convertAngle = mTankTransform.localScale.x >= 0 ? 0 : 180; | |
| Vector3 euler = mTurretTransform.eulerAngles; | |
| euler.z = angle + convertAngle; | |
| mTurretTransform.eulerAngles = euler; | |
| } | |
| private void UpdateStateWaitForInput() | |
| { | |
| if(Input.GetMouseButtonDown(0)){ | |
| //Vector2 pressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); | |
| if(IsTouchingMe(Input.mousePosition)) { | |
| mPressPosition = GetReferencePosition(); | |
| mHasPressed = true; | |
| mMoveCancelled = false; | |
| } else { | |
| mHasPressed = false; | |
| } | |
| } | |
| if(mHasPressed) { | |
| mArrow.SetVisible(true); | |
| if(Input.GetMouseButton(0)) { | |
| HandleRotateByMouse(); | |
| } | |
| if(Input.GetMouseButtonUp(0)){ | |
| if(! mMoveCancelled) { | |
| LaunchBullet(); | |
| } | |
| mArrow.SetVisible(false); | |
| mMoveCancelled = false; | |
| mHasPressed = false; | |
| } | |
| } | |
| } | |
| void LaunchBullet() | |
| { | |
| Transform parent = transform.parent; | |
| GameObject bulletObject = GameObject.Instantiate(bulletPrefab, parent, true); | |
| bulletObject.transform.position = mLaunchPositionTransform.transform.position; | |
| Bullet bullet = bulletObject.GetComponent<Bullet>(); | |
| bullet.Launch(mPower, GetTurretAngle()); | |
| //testBullet.Launch(power, testTank.GetTurretAngle()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class TankLinkPrefab : Tank { | |
| public AimArrow aimArrowPrefab; | |
| override protected void SetupAimArrow() { | |
| // Create an actual object | |
| Transform parent = transform; | |
| GameObject aimObject = GameObject.Instantiate(aimArrowPrefab.gameObject, transform, false); | |
| aimObject.transform.localPosition = GetAimArrowTransformPosition(); | |
| mArrow = aimObject.GetComponent<AimArrow>(); | |
| mArrow.SetVisible(false); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment