Top down rotation using mouse (Two Options 1. Fix my code 2. find a way to have 3D walls
So basicaly what iam trying to do is create a top down shooter which use the mouse to aim. However my code does not work the way
the code
using System.Collections; using System.Collections.Generic; using Mono.Cecil.Cil; using UnityEngine; using UnityEngine.UIElements;
public class Goon : MonoBehaviour { [Header("Gun Settings")] public Transform Gun; public Transform RougeLikeParentPlayer; public PlayerMovement RougeLikeParentPlayer_Movement; public float FireRate = 0.1f; public int clipSize = 30; public int ReservedAmmoCapacity = 270;
[Header("Gun Variables")]
public bool _Equipeded = false;
public bool _AutoAim;
public bool _FullAuto;
public bool _canShoot;
public int _Damage;
public int _CurrentAmmoInClip;
public int _AmmoInReserve;
public float _Knockback;
public LayerMask EnemyMask;
[Header("Shoot")]
public KeyCode ShootKey = KeyCode.Mouse1;
public GameObject Bullet;
public Bullet BulletScript;
public GameObject BulletPrefab;
public GameObject ShootPoint;
public float BulletSpeed = 1.5f;
[Header("Aim")]
public Rigidbody2D Rb;
public Transform Enemy;
public bool FacingRight = true;
public float RotationZ;
public bool DoNotCallUntilFinished = false;
// Start is called before the first frame update
void Start()
{
Rb = this.GetComponent<Rigidbody2D>();
RougeLikeParentPlayer_Movement = RougeLikeParentPlayer.GetComponent<PlayerMovement>();
}
// Update is called once per frame
void FixedUpdate()
{
if (_Equipeded == true)
{
if (_AutoAim == true)
{
}
else if (_AutoAim == false)
{
Vector3 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 Direction = MousePos - transform.position;
Direction.Normalize();
RotationZ = Mathf.Atan2(Direction.y, Direction.x) * Mathf.Rad2Deg;
if (!FacingRight)
{
transform.localRotation = Quaternion.Euler(0, 0, RotationZ);
}
else if (FacingRight)
{
transform.localRotation = Quaternion.Euler(0, 0, -RotationZ - 180);
}
if (MousePos.x < RougeLikeParentPlayer.position.x && !FacingRight)
{
Flip();
}
else if (MousePos.x > RougeLikeParentPlayer.position.x && FacingRight)
{
Flip();
}
}
if (_canShoot == true)
{
if (_FullAuto == false && Input.GetKeyDown(ShootKey))
{
Bullet = Instantiate(BulletPrefab, ShootPoint.transform.position, transform.rotation);
BulletScript = Bullet.GetComponent<Bullet>();
BulletScript.Shooter = Rb;
BulletScript.BulletSpeed = BulletSpeed;
}
if (_FullAuto == true && Input.GetKey(ShootKey))
{
StartCoroutine(Shoot());
}
}
}
}
IEnumerator Shoot()
{
_canShoot = false;
yield return new WaitForSeconds(FireRate);
Bullet = Instantiate(BulletPrefab, ShootPoint.transform.position, transform.rotation);
BulletScript = Bullet.GetComponent<Bullet>();
BulletScript.Shooter = Rb;
BulletScript.BulletSpeed = BulletSpeed;
_canShoot = true;
}
int i;
void Flip()
{
i++;
Debug.Log("Flip " + i);
FacingRight = !FacingRight;
DoNotCallUntilFinished = false;
RougeLikeParentPlayer_Movement.CalledFromObject = this.gameObject;
RougeLikeParentPlayer_Movement.PlayerDirection();
}
IEnumerator Delay(float Delay, bool DelayBool)
{
yield return new WaitForSeconds(Delay);
DelayBool = false;
}
}
I know the issue of the code but i cant fix it. I Currently have my camera set to persecutive not orthographic which causes the gun not to rotate. there are two ways to solve this but I am not sure on how to execute them i A have to find a way to deal with the Extra value or B need to find a way to make my walls look 3d using orthographic (i believe shaders?) plz help Ive been on this for two weeks now
Answer by dr-Eggs · Mar 16 at 09:00 PM
okay so ive recreated my code however there is still one problem [Header("Gun Settings")] public Transform Gun; public Transform RougeLikeParentPlayer; public PlayerMovement RougeLikeParentPlayer_Movement; public float FireRate = 0.1f; public int clipSize = 30; public int ReservedAmmoCapacity = 270;
[Header("Gun Variables")]
public bool _Equipeded = false;
public bool _AutoAim;
public bool _FullAuto;
public bool _canShoot;
public int _Damage;
public int _CurrentAmmoInClip;
public int _AmmoInReserve;
public float _Knockback;
public LayerMask EnemyMask;
[Header("Shoot")]
public KeyCode ShootKey = KeyCode.Mouse1;
public GameObject Bullet;
public Bullet BulletScript;
public GameObject BulletPrefab;
public GameObject ShootPoint;
public float BulletSpeed = 1.5f;
[Header("Aim")]
public Rigidbody2D Rb;
public Transform Enemy;
public bool FacingRight = true;
public float RotationZ;
public bool DoNotCallUntilFinished = false;
public Camera OthoCam;
// Start is called before the first frame update
void Start()
{
Rb = this.GetComponent<Rigidbody2D>();
RougeLikeParentPlayer_Movement = RougeLikeParentPlayer.GetComponent<PlayerMovement>();
}
// Update is called once per frame
void FixedUpdate()
{
if (_Equipeded == true)
{
if (_AutoAim == true)
{
}
else if (_AutoAim == false)
{
Vector3 MousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
MousePos = Camera.main.ScreenToWorldPoint(MousePos);
Debug.Log(MousePos);
Ray CamRay = Camera.main.ScreenPointToRay(MousePos);
Plane GroundPlane = new Plane(Vector3.forward, Vector3.zero);
float RayLength;
if (GroundPlane.Raycast(CamRay, out RayLength))
{
Vector3 PointToLook = CamRay.GetPoint(RayLength);
Debug.DrawLine(CamRay.origin, PointToLook, Color.blue);
RotationZ = (Mathf.Atan2(PointToLook.x, PointToLook.y) * Mathf.Rad2Deg) + 90;
Debug.Log(RotationZ);
if (FacingRight)
{
transform.localRotation = Quaternion.Euler(0, 0, RotationZ);
}
else if (!FacingRight)
{
transform.localRotation = Quaternion.Euler(0, 0, -RotationZ - 180);
}
}
if (MousePos.x < RougeLikeParentPlayer.position.x && !FacingRight)
{
Flip();
}
else if (MousePos.x > RougeLikeParentPlayer.position.x && FacingRight)
{
Flip();
}
}
if (_canShoot == true)
{
if (_FullAuto == false && Input.GetKeyDown(ShootKey))
{
Bullet = Instantiate(BulletPrefab, ShootPoint.transform.position, transform.rotation);
BulletScript = Bullet.GetComponent<Bullet>();
BulletScript.Shooter = Rb;
BulletScript.BulletSpeed = BulletSpeed;
}
if (_FullAuto == true && Input.GetKey(ShootKey))
{
StartCoroutine(Shoot());
}
}
}
}
IEnumerator Shoot()
{
_canShoot = false;
yield return new WaitForSeconds(FireRate);
Bullet = Instantiate(BulletPrefab, ShootPoint.transform.position, transform.rotation);
BulletScript = Bullet.GetComponent<Bullet>();
BulletScript.Shooter = Rb;
BulletScript.BulletSpeed = BulletSpeed;
_canShoot = true;
}
int i;
void Flip()
{
i++;
Debug.Log("Flip " + i);
FacingRight = !FacingRight;
DoNotCallUntilFinished = false;
RougeLikeParentPlayer_Movement.CalledFromObject = this.gameObject;
RougeLikeParentPlayer_Movement.PlayerDirection();
}
IEnumerator Delay(float Delay, bool DelayBool)
{
yield return new WaitForSeconds(Delay);
DelayBool = false;
}
}
the only problem is that if i leave my mouse idle and then move the mouse position does not up date and stays as the old mouse position until i move my mouse but then its still moving from the old position for example i stop moving my mouse at (5,18,8) so i move my character down by five so now the mouse pos should be (5,13,8) but it stays as (5,18,8) then say i move my mouse right by five instead of being (10,13,8) its (10,18,8). i believe this is because it is not to world point? even thought i do use screen to world point as well as the fact that it does not constantly update? only when the mouse is moved by the user?