2D gun aiming problem
Hi, I'm working on a 2d game where the player holds a weapon and aim /flip with the cursor.
When the player is flipped, the rotation / aiming with the mouse isn't fluid anymore, the player is not able to do a full range of rotation.
Can someone help me with that ? See script below
weaponcontroller.txt
(15.8 kB)
Comment
Best Answer
Answer by Vuvuvi · Jan 26 at 02:22 PM
Code Snippet
if (_orientation.x < 0 && _orientation.y < transform.localPosition.y)
_direction = (_camera.ScreenToWorldPoint(temp) - transform.position).normalized;
else
_direction = (_camera.ScreenToWorldPoint(temp) - transform.position + _recoilOffsetLerped).normalized;
_direction.x = _orientation.x;
_direction.y = _orientation.x;*/
float tempAngle = Mathf.Atan2(_direction.y, _direction.x) * Mathf.Rad2Deg;
if (_orientation.x < 0)
{
GameState._isCharacterFlipped = true;
_parent.transform.localScale = new Vector3(-1, 1, 1);
if (_shootFX != null)
{
_shootFXTransform.localEulerAngles = new Vector3(0, 180, 0);
}
if (_orientation.y < transform.localPosition.y)
transform.eulerAngles = new Vector3(0, 0, (Mathf.Clamp(tempAngle, -180, -180 + _aimAngleRange / 2)) - 180);
else
transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp(tempAngle, 180 - _aimAngleRange / 2, 180) - 180 );
}
else
{
GameState._isCharacterFlipped = false;
if (_shootFX != null)
_shootFXTransform.localEulerAngles = new Vector3(0, 0, 0);
_parent.transform.localScale = new Vector3(1, 1, 1);
transform.eulerAngles = new Vector3(0, 0, Mathf.Clamp(tempAngle, -_aimAngleRange / 2, _aimAngleRange / 2));
}
if (Input.GetKey(KeyCode.Mouse1) || Input.GetAxis("Aim") == 1.0f )
{
if (_magazineCapacity > 0)
_canShoot = true;
if (_pointLightStruct.Length > 0)
{
for (int i = 0; i < _pointLightStruct.Length; i++)
{
_toAdd = (_pointLightStruct[i]._maxLightAngle - _pointLightStruct[i]._minLightAngle) * Time.deltaTime* _aimingSpeed;
_pointLightStruct[i]._light.pointLightInnerAngle = Mathf.Clamp(_pointLightStruct[i]._light.pointLightInnerAngle -= _toAdd, _pointLightStruct[i]._minLightAngle, _pointLightStruct[i]._maxLightAngle);
IEnumerator ShootWithSpread(float spreadAngle, int numberOfPellets, Quaternion transformRotation, Vector3 spawnerPosition, float powerMulti = 1f, float duration = 1f)
{
if(_shootFX!=null)
{
_shootFX.Play();
Recoil();
}
for (int i = 0; i < numberOfPellets; i++)
{
GameObject pellet = Instantiate(_projectile, spawnerPosition, transformRotation);
if(GameState._isCharacterFlipped)
pellet.transform.rotation = Quaternion.Euler(new Vector3(0, 0, transformRotation.eulerAngles.z + Random.Range(-spreadAngle, spreadAngle)+180));
else
pellet.transform.rotation = Quaternion.Euler(new Vector3(0, 0, transformRotation.eulerAngles.z + Random.Range(-spreadAngle, spreadAngle)));
pellet.GetComponent<Rigidbody2D>().AddForce(pellet.transform.right * _firePower * powerMulti);
Destroy(pellet, duration);
yield return new WaitForSeconds(.01f);
}
Your answer
Follow this Question
Related Questions
Mouse Follow Aim with IK - 2D Bowman 0 Answers
HELP 2D Top Down Shooter Bullets Only go Horizontal 0 Answers
why wont my ship fire alternating guns? 0 Answers
Aim at my cursor 1 Answer
3D Side Scroller Gun PickUp 0 Answers