// Update is called once per frame void Update() { timeElapsed += Time.deltaTime; Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 dir = pos - transform.position; //if(facingRight != playerCtrl.facingRight) // Flip (); float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward); //angle = Mathf.Atan2(dir.y, facingRight ? -dir.x : dir.x) * Mathf.Rad2Deg; //if(!facingRight) // transform.localRotation = Quaternion.AngleAxis(angle, Vector3.forward); if (Input.GetMouseButton(0) && !this.GetComponentInParent <PlayerHealth>().isDead&& timeElapsed >= cooldown) { //Gun flare effect Instantiate(gunFlare, transform.position, transform.rotation); //Shoot out a gun Rigidbody2D gunBeamInstance = Instantiate(gunBeam, new Vector3(transform.position.x, transform.position.y, 0f), transform.localRotation) as Rigidbody2D; dir.z = 0; dir.Normalize(); gunBeamInstance.velocity = new Vector2(speed * dir.x, speed * dir.y); gunBeamInstance.GetComponentInParent <SpriteRenderer>().color = currColor; BasicGunBeam currBeam = gunBeamInstance.GetComponentInParent <BasicGunBeam>(); currBeam.direction = dir; currBeam.speed = speed; currBeam.color = currColor; currBeam.multiplier = multiplier; timeElapsed = 0; } }
// Update is called once per frame void Update() { speed = gunSpeeds[SelectGunScript.index % 4]; moveVelocity = transform.right * speed; if (!computedLogistic) { logistic = computeLogisticFunction() + 0.4f; computedLogistic = true; } // activate/deactivate mud if (!mudActive && levelScript.level >= 1 && logistic > 0.5 && TimerScript.time > 2 && CoinScript.coinCount >= 2) { activateMud(); } else if (TimerScript.time < 2) { mud.SetActive(false); mudActive = false; } // create mud effect if (mudActive && Mathf.Abs(rb.GetComponentInParent <RectTransform>().anchoredPosition.x - mud.GetComponent <RectTransform>().anchoredPosition.x) <= ((mud.GetComponent <RectTransform>().rect.width *mud.GetComponent <RectTransform>().localScale.x) / 2)) { moveVelocity *= 0.5f; } // x2 speed boost if (speedBoosted) { moveVelocity *= 2; } if (moveRight && TimerScript.keepTiming) { MoveRight(); } /*else if (Input.touchCount > 0 && TimerScript.keepTiming) * { * if (Input.GetTouch(0).deltaPosition.x > 0) * { * MoveRight(); * } * }*/ else if (moveLeft && TimerScript.keepTiming) { MoveLeft(); } /*else if (Input.touchCount > 0 && TimerScript.keepTiming) * { * if (Input.GetTouch(0).deltaPosition.x < 0) * { * MoveLeft(); * } * }*/ }
public void applyDamage(float value, Vector2 x) { rb2d.GetComponentInParent <PlayerController>().soundSource.clip = rb2d.GetComponentInParent <PlayerController>().hitSound; rb2d.GetComponentInParent <PlayerController>().soundSource.Play(); damageTaken += value; rb2d.AddForce(x * damageTaken); hitCheck = 1; StartCoroutine(resetHitCheck()); }
// Start is called before the first frame update void Start() { speed = gunSpeeds[SelectGunScript.index % 4]; rb = GameObject.Find("DGT Handgun 4").GetComponent <Rigidbody2D>(); mud.SetActive(false); left = GameObject.Find("MoveLeft").GetComponent <Button>(); logistic = 0; rb.GetComponentInParent <RectTransform>().anchoredPosition = new Vector2(rb.GetComponentInParent <RectTransform>().anchoredPosition.x, GameObject.Find("Shoot Button").GetComponent <RectTransform>().anchoredPosition.y + (rb.GetComponentInParent <RectTransform>().rect.height *rb.GetComponentInParent <RectTransform>().localScale.y)); computedLogistic = false; }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Pot" && this.gameObject.tag == "Hit") { collision.GetComponent <Pot>().Destroy(); } if (collision.gameObject.CompareTag("Player") || collision.gameObject.CompareTag("Enemy") && collision.isTrigger) { Rigidbody2D hit = collision.GetComponentInParent <Rigidbody2D>(); if (hit != null) { Vector2 difference = (hit.transform.position - transform.position).normalized * thrust; hit.DOMove(difference + hit.position, knockTime); if (collision.gameObject.tag == "Enemy") { collision.GetComponent <Enemy>().Knock(hit, knockTime); hit.GetComponent <Enemy>().currentState = EnemyState.stagger; } if (collision.gameObject.tag == "Player") { collision.GetComponentInParent <PlayerMove>().Knock(knockTime); hit.GetComponentInParent <PlayerMove>().currentState = PlayerState.stagger; } } } }
//-------------------------------------------------------- void Execute() { // Notify basket Basket b = rb.GetComponentInParent <Basket>(); b.OnThrow(); // Reset PhysX rb.transform.SetParent(null, true); rb.velocity = Vector2.zero; rb.angularVelocity = 0f; // Throw Vector2 direction = b.GetThrowDirection(rb, strength); rb.simulated = true; rb.AddForce(direction, ForceMode2D.Impulse); rb.AddTorque(strength); SoundController.PlaySFX("throw"); if (trajectory != null) { trajectory.OnThrow(); } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Breakable")) { collision.GetComponent <GoldJar>().KillObject(); } if (collision.gameObject.CompareTag("EnemyHitBox")) { Rigidbody2D hit = collision.GetComponentInParent <Rigidbody2D>(); if (hit != null) { //empuja al objeto en la direccion opuesta Vector3 difference = hit.transform.position - transform.position; difference = difference.normalized * knockPower; hit.AddForce(difference, ForceMode2D.Impulse); if (collision.gameObject.CompareTag("EnemyHitBox") && collision.isTrigger) { hit.GetComponentInParent <Enemy>().currentEnemyState = EnemyState.stagger; collision.GetComponentInParent <Enemy>().KnockBack(hit, knockTime); collision.GetComponentInParent <Enemy>().TakeDamage(playerDamage, knockTime); } } } }
public override void AttachToRB(Rigidbody2D attachedTo, PlayerController pc) { if (!isAttached) { Piece p = attachedTo.GetComponent <Piece>(); if (p == null) { p = attachedTo.GetComponentInParent <Piece>(); } if (p != null) { RelativeJoint2D fj2d = gameObject.AddComponent <RelativeJoint2D>(); fj2d.connectedBody = attachedTo; fj2d.breakForce = breakForce; fj2d.correctionScale = 0.25f; activeJoints.Add(fj2d); current_pc = pc; isAttached = true; p.piecesAttachedToMe.Add(this); parentPiece = p; if (current_pc != null) { current_pc.AddPower(power); } } } }
void init() { positions = new Vector2[chainLength + 1]; bones = new AnchoredJoint2D[chainLength + 1]; bonesT = new Transform[chainLength + 1]; bonesR = new Rigidbody2D[chainLength + 1]; StartDir = new Vector2[chainLength + 1]; bonesLength = new float[chainLength]; avAngles = new float[chainLength]; angleOffset = new float[chainLength]; completeLength = 0; var current = GetComponent <AnchoredJoint2D> (); if (target) { var controller = target.GetComponent <IKController> (); if (!controller) { controller = target.gameObject.AddComponent <IKController> (); } controller.Init(this); } for (int i = chainLength; i >= 0; i--) { bones[i] = current; bonesT[i] = current.transform; bonesR[i] = current.attachedRigidbody; if (i == chainLength) { StartDir[i] = (Vector2)target.position - getBonePos(i); } else { var dir = getBonePos(i + 1) - getBonePos(i); StartDir[i] = dir; bonesLength[i] = dir.magnitude; completeLength += bonesLength[i]; } current = current.connectedBody.GetComponent <AnchoredJoint2D> (); } root = bones[0].connectedBody.transform; rootrb = root.GetComponent <Rigidbody2D> (); if (!rootrb) { rootrb = rootrb.GetComponentInParent <Rigidbody2D> (); } for (int i = 1; i <= chainLength; i++) { avAngles[i - 1] = PlainMath.AngleBetween(getBonePos(i), root.position); angleOffset[i - 1] = PlainMath.AngleFromDirection(getBonePosVR2(i - 1) - getBonePosVR2(i)) - root.eulerAngles.z; //Debug.Log(angleOffset[i - 1] + " , " + bonesT[i].name + " :: " + root.eulerAngles.z + " :: " + bonesT[i].eulerAngles.z); //Debug.Log(AngleFromDirection((Vector2)root.position - getBonePos(i)) + " , " + bonesT[i].na); } //Debug.Log(root + " , " + rootrb); }
public void OnTriggerEnter2D(Collider2D other) { foreach (string collisionTag in collisionTags) { if (other.gameObject.CompareTag(collisionTag) && other.isTrigger) { Rigidbody2D hit = other.GetComponentInParent <Rigidbody2D>(); if (hit != null) { // print("IsRigid"); Vector3 difference = (hit.transform.position - transform.position) * Time.deltaTime; // Debug.DrawLine(hit.transform.position, transform.position, Color.red); difference = difference.normalized * thrust; //hit.MovePosition(difference); // hit.DOMove(hit.transform.position + difference, knockTime); if (hit.bodyType == RigidbodyType2D.Dynamic) { hit.AddForce(difference, ForceMode2D.Impulse); //StartCoroutine(FlyEffectCo(knockTime, other)); } else { hit.bodyType = RigidbodyType2D.Dynamic; hit.AddForce(difference, ForceMode2D.Impulse); } if (other.gameObject.CompareTag("EnemyHealth") && other.isTrigger) { hit.GetComponentInParent <Enemy>().Knock(hit, knockTime); } else if (other.gameObject.CompareTag("PlayerHealth")) { if (other.GetComponentInParent <Movement>() != null && other.GetComponentInParent <Movement>().currentState != PlayerState.stagger) { hit.GetComponentInParent <Movement>().Knock(knockTime); } } } } } }
private void Start() { screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z)); GameObject player = GameObject.FindGameObjectWithTag("Player"); playerRefRB = player.GetComponent <Rigidbody2D>(); content = GameObject.FindGameObjectWithTag("Content").GetComponent <Image>(); cc = playerRefRB.GetComponentInParent <CircleCollider2D>(); explosionRef = Resources.Load("Explosion"); }
/// <summary> /// compares the tags and sends the correct value to the coroutine in player or enemy. /// </summary> /// <param name="collision"></param> private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Enemy") && collision.isTrigger) { Rigidbody2D target = collision.GetComponentInParent <Rigidbody2D>(); if (target != null) { Vector3 difference = target.transform.position - transform.position; difference = difference.normalized * knockback; target.DOMove(target.transform.position + difference, time); target.GetComponentInParent <EnemyController>().stateMachine.ChangeState(EnemyState.STAGGER); collision.GetComponentInParent <EnemyController>().KnockBack(time); } } }
/// <summary> /// compares the tags and sends the correct value to the coroutine in player or enemy. /// </summary> /// <param name="collision"></param> private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Player") && collision.isTrigger) { Rigidbody2D target = collision.GetComponentInParent <Rigidbody2D>(); if (target != null) { Vector3 difference = transform.position - collision.transform.position; difference = difference.normalized * knockback; target.DOMove(target.transform.position + difference, time); if (collision.GetComponentInParent <PlayerController>().stateMachine.currentPlayerState != PlayerState.stagger) { target.GetComponentInParent <PlayerController>().stateMachine.currentPlayerState = PlayerState.stagger; collision.GetComponentInParent <PlayerController>().KnockBack(time); } } } }
/*private void OnTriggerEnter2D(Collider2D collision) * { * if (collision.gameObject.CompareTag("PlayerHitBox")) * { * Rigidbody2D hit = collision.GetComponentInParent<Rigidbody2D>(); * * if (hit != null) * { * //empuja al objeto en la direccion opuesta * Vector3 difference = hit.transform.position - transform.position; * difference = difference.normalized * knockPower; * hit.AddForce(difference, ForceMode2D.Impulse); * } * } * }*/ private void OnTriggerStay2D(Collider2D collision) { if (collision.gameObject.CompareTag("PlayerHitBox")) { Rigidbody2D hit = collision.GetComponentInParent <Rigidbody2D>(); if ((hit != null) && attackSpeedCountdown <= 0) { if (collision.gameObject.CompareTag("PlayerHitBox") && collision.isTrigger) { if (collision.GetComponentInParent <PlayerMovement>().playerCurrentState != PlayerState.stagger) { hit.GetComponentInParent <PlayerMovement>().playerCurrentState = PlayerState.stagger; collision.GetComponentInParent <PlayerMovement>().KnockBack(knockTime); collision.GetComponentInParent <PlayerMovement>().TakeDamage(enemyDamage); attackSpeedCountdown = attackSpeed; } } } } }
// Start is called before the first frame update public override void AttachToRB(Rigidbody2D attachedTo, PlayerController pc = null) { if (!isAttached) { Piece p = attachedTo.GetComponent <Piece>(); if (p == null) { p = attachedTo.GetComponentInParent <Piece>(); } if (p != null) { FixedJoint2D fj2d = gameObject.AddComponent <FixedJoint2D>(); fj2d.connectedBody = attachedTo; fj2d.breakForce = breakForce; activeJoints.Add(fj2d); isAttached = true; p.piecesAttachedToMe.Add(this); parentPiece = p; } } }
//-------------------------------------------------------- public void OnStickLongEnough() { if (stickyObject == null) { return; } Rigidbody2D so = stickyObject; stickyObject = null; so.GetComponentInParent <Egg>().OnStickTo( GetComponentInParent <Basket>() ); so.simulated = false; Transform t = so.transform; t.SetParent(transform, true); // t.localPosition = Vector3.zero; }
private Rigidbody2D FindNearestOrbit() { if (!gameManager.TryGetComponent <StarSpawner>(out StarSpawner spawner)) { return(null); } Rigidbody2D nearestOrbitAnchor = null; float nearestOrbitAnchorDistance = float.MaxValue; foreach (StarInitializer star in spawner.SpawnedObjects) { if (!star.TryGetComponent <Orbitable>(out Orbitable orbitable)) { break; } foreach (Orbiter orbit in orbitable.Orbits) { if (orbit.IsOccupied) { break; } float distance = Vector2.Distance(transform.position, orbit.transform.position); if (distance < nearestOrbitAnchorDistance && orbit.Anchor.TryGetComponent <Rigidbody2D>(out Rigidbody2D rb)) { nearestOrbitAnchor = rb; nearestOrbitAnchorDistance = distance; } } } if (nearestOrbitAnchor != null) { nearestOrbitAnchor.GetComponentInParent <Orbiter>().IsOccupied = true; } return(nearestOrbitAnchor); }
/* * void LateUpdate() * { * //Testing solo * if (isAttached) * { * float actionValue = Input.GetAxis("Horizontal"); * rb.AddTorque(-actionValue * baseTurnPower); * } * }*/ public override void AttachToRB(Rigidbody2D attachedTo, PlayerController pc = null) { if (!isAttached) { Piece p = attachedTo.GetComponent <Piece>(); if (p == null) { p = attachedTo.GetComponentInParent <Piece>(); } if (p != null) { p.piecesAttachedToMe.Add(this); DistanceJoint2D dj2d1 = gameObject.AddComponent <DistanceJoint2D>(); DistanceJoint2D dj2d2 = gameObject.AddComponent <DistanceJoint2D>(); DistanceJoint2D dj2d3 = gameObject.AddComponent <DistanceJoint2D>(); dj2d1.connectedBody = attachedTo; dj2d2.connectedBody = attachedTo; dj2d3.connectedBody = attachedTo; dj2d1.breakForce = breakForce; dj2d2.breakForce = breakForce; dj2d3.breakForce = breakForce; Vector2 dist = transform.position - attachedTo.transform.position; Vector2 offset = new Vector2(dist.y, -dist.x); dj2d1.connectedAnchor += offset; dj2d2.connectedAnchor -= offset; dj2d3.connectedAnchor += dist; activeJoints.Add(dj2d1); activeJoints.Add(dj2d2); activeJoints.Add(dj2d3); isAttached = true; p.piecesAttachedToMe.Add(this); parentPiece = p; } } }
public void LaunchBall() { if (HoldingBall) { _heldBall.simulated = true; _heldBall.isKinematic = false; _heldBall.transform.SetParent(null); Ball ball = _heldBall.GetComponentInParent <Ball>(); var paddle = GetComponentInParent <PaddleController>(); ball.ChangeSpeedAndSetDirection(_launchStrength + paddle.SpeedIncreasePerBounce, GetOffsetLaunchDirection()); _heldBall.GetComponentInChildren <TrailRenderer>().emitting = true; _heldBall = null; _launchAngleOffset = 0; _timeHeld = 0; _launchDirectionVisualizer.localRotation = Quaternion.identity; } }
private void Start() { //rb.GetComponent<Rigidbody2D>(); rb.GetComponentInParent <Rigidbody2D>(); }