From e78ac3984119774580720c48634309250a3c74f5 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 20 Nov 2020 00:48:50 +0100 Subject: [PATCH] Aiming with the mouse now --- Assets/Animations/Character.controller | 22 ++++++++++++++++++++++ Assets/Scripts/OwnCharacterController.cs | 5 +++-- Assets/Scripts/RocketScript.cs | 7 ++++--- Assets/Scripts/WeaponFireController.cs | 19 +++++++++++++++++-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Assets/Animations/Character.controller b/Assets/Animations/Character.controller index 16daa9f..1f73d08 100644 --- a/Assets/Animations/Character.controller +++ b/Assets/Animations/Character.controller @@ -59,6 +59,28 @@ AnimatorStateTransition: m_InterruptionSource: 0 m_OrderedInterruption: 0 m_CanTransitionToSelf: 0 +--- !u!1101 &-2480231214708551907 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: [] + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: 0} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 1 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.28571433 + m_HasExitTime: 1 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 --- !u!1102 &-1640697823006375366 AnimatorState: serializedVersion: 5 diff --git a/Assets/Scripts/OwnCharacterController.cs b/Assets/Scripts/OwnCharacterController.cs index a510ddf..78d0d14 100644 --- a/Assets/Scripts/OwnCharacterController.cs +++ b/Assets/Scripts/OwnCharacterController.cs @@ -46,7 +46,8 @@ public class OwnCharacterController : CharacterControllerBase tr.localScale = scale; } - if (Input.GetButton("Fire3")) + bool sprinting = Input.GetButton("Fire3"); + if (sprinting) input *= sprintSpeed; if (Mathf.Abs(_rb.velocity.x) <= 3) @@ -56,7 +57,7 @@ public class OwnCharacterController : CharacterControllerBase { //_rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); _rb.velocity += new Vector2(0, jumpForce); - if (Input.GetButton("Fire3")) //Csak akkor animálja az ugrást, ha fut közben + if (sprinting) //Csak akkor animálja az ugrást, ha fut közben { _jumpStatus = JumpStatus.Up; _animator.SetInteger(Jump, (int) _jumpStatus); diff --git a/Assets/Scripts/RocketScript.cs b/Assets/Scripts/RocketScript.cs index 4295b1c..eff5599 100644 --- a/Assets/Scripts/RocketScript.cs +++ b/Assets/Scripts/RocketScript.cs @@ -7,8 +7,8 @@ public class RocketScript : MonoBehaviour { private float _fired; private Rigidbody2D _rb; - private bool _goingRight; private byte _hitCount; + private Vector2 _forward; public byte maxHits = 3; @@ -21,8 +21,8 @@ public class RocketScript : MonoBehaviour private void OnEnable() { _fired = Time.fixedTime; - _goingRight = transform.localScale.x > 0; _hitCount = 0; + _forward = transform.right; } // Update is called once per frame @@ -35,7 +35,8 @@ public class RocketScript : MonoBehaviour _rb.velocity = Vector2.zero; } - _rb.AddForce(new Vector2(_goingRight ? 10 : -10, 0)); + //_rb.AddForce(new Vector2(_goingRight ? 10 : -10, 0)); + _rb.AddForce(10 * _forward); } private void OnCollisionEnter2D(Collision2D other) diff --git a/Assets/Scripts/WeaponFireController.cs b/Assets/Scripts/WeaponFireController.cs index ac03000..77407e0 100644 --- a/Assets/Scripts/WeaponFireController.cs +++ b/Assets/Scripts/WeaponFireController.cs @@ -23,10 +23,25 @@ public class WeaponFireController : MonoBehaviour var theRocket = _pool.GetObject(); var rocketTransform = theRocket.transform; rocketTransform.position = firePoint.position; - var scale = rocketTransform.localScale; + /*var scale = rocketTransform.localScale; if (transform.localScale.x * scale.x < 0) scale.x *= -1; - rocketTransform.localScale = scale; + rocketTransform.localScale = scale;*/ + + // vector from this object towards the target location + var vectorToTarget = Camera.main.ScreenToWorldPoint(Input.mousePosition) - rocketTransform.position; + // rotate that vector by 90 degrees around the Z axis + Vector3 rotatedVectorToTarget = Quaternion.Euler(0, 0, 90) * vectorToTarget; + + // get the rotation that points the Z axis forward, and the Y axis 90 degrees away from the target + // (resulting in the X axis facing the target) + Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, rotatedVectorToTarget); + + rocketTransform.rotation = targetRotation; + Debug.Log("Rotation: " + rocketTransform.rotation.eulerAngles); + /*Debug.Log("Rocket position: " + rocketTransform.position); + Debug.Log("Target position: " + Camera.main.ScreenToWorldPoint(Input.mousePosition)); + Debug.Log("Difference: " + direction);*/ theRocket.SetActive(true); } }