Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dr-Eggs · Mar 12 at 06:02 PM · rotationmousewallorthographic cameraperspective camera

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Orthographic Camera for objects but perspective for GUI? 1 Answer

Rotate player with mouse. 0 Answers

How to make Rotation of HingeJoint2D constant via mouse button C# 1 Answer

Limiting localrotation between 2 values 1 Answer

Camera rotation with mouse cursor 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges