Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by AmazingMrBrock · Jul 20, 2014 at 07:06 PM · cameraraycastinput.mouseposition

Weird mouse look glitch in top down shooter.

As said above I am working on a top down shooter, I have it set up so that I control my unit with wasd and the unit tracks the mouse to aim.

My mouse tracker is set up so that a ray is sent outwards from the mouse point on screen. It then collides with a collider and layer mask thats strictly used for the unit targeting.

Now my issues is that the collider is attached to my unit, and visibly follows the unit around when I move the unit using movement keys. However if my unit leaves the area it started in it is no longer able to aim the mouse, it seems to be not returning any point data. Which makes me think that for some reason my code is retaining the original position of the collider. Also if I walk backwards a bit my unit starts glitching out and spinning around as long as I'm holding the back button, only backwards every other direction works better. So I'm here to see if anyone can help.

Playerscript to follow, there is also a camera script but its fairly simple and only effects the camera. If you want to see it I can copy that in as well.

 public class PlayerScript : MonoBehaviour {
 public static PlayerScript instance;
     CameraMover cM;
 
     float acceleration = 2f;
     float maxSpeed = 2f;
     float jump = 5f;
     
     Rect box;
     Vector2 velocity;
     BoxCollider boxCollider;
     Transform holder;
 
     bool walled;
     
     int margin = 2;
 
     Vector3 mouseHit;
     Vector3 unitPos;
     float angle;
 
     public float moveForce = 3f;
     RaycastHit hit; 
     RaycastHit hitInfo = new RaycastHit();
     public LayerMask rayMask = -9;
 
     /// <summary>
     /// TODO
     /// Movement controls
     /// raycasting
     /// weapon
     /// TOREAD
     /// 
     /// </summary>
     
 
     void Awake(){
         instance = this;
     }
 
     void Update(){
 
         CameraMover.instance.SetTarget(transform);
         RotateToMouse();
     }
 
     void FixedUpdate (){
         unitPos.x = transform.position.x + 2f; unitPos.y = transform.position.y + 2f;
         RaycastLoop(0.04f, unitPos, velocity.x);
         DirectionalMovement();
 
 
     }
 
     //PROBLEm
     //I think it has something to do with game not recognise that the mouse box collision layer and or mouse has moved in some regards
     // which make it only work where you spawn.
 
     private void RotateToMouse(){   
         UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit , 200, rayMask)){
             mouseHit = hit.point;
             DebugLogger();
         }
 
         mouseHit.Normalize();
         float rotZ = Mathf.Atan2 (mouseHit.y, mouseHit.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
     }
 
     void DirectionalMovement(){
         float horizontalAxis = Input.GetAxisRaw("Horizontal");
         float verticalAxis = Input.GetAxisRaw("Vertical");
 
         velocity = new Vector2(Mathf.Max(velocity.x = acceleration, -maxSpeed), Mathf.Max(velocity.y = acceleration, -maxSpeed));
 
         Vector3 hDirection = Vector3.left * horizontalAxis;
         Vector3 vDirection = Vector3.up * verticalAxis;
         
         if(horizontalAxis != 0){
             if(hitInfo.distance == 0){
                 transform.Translate((hDirection * 10) * Time.deltaTime);
             }
             if(Mathf.Abs(rigidbody.velocity.x) > maxSpeed)
                 rigidbody.velocity = new Vector2(Mathf.Sign(rigidbody.velocity.x) * maxSpeed, rigidbody.velocity.y);
         }
 
         if(verticalAxis != 0){
             if(hitInfo.distance == 0){
                 transform.Translate((vDirection * 10) * Time.deltaTime);
             }
             if(Mathf.Abs(rigidbody.velocity.x) > maxSpeed)
                 rigidbody.velocity = new Vector2(Mathf.Sign(rigidbody.velocity.x) * maxSpeed, rigidbody.velocity.y);
         }
     }
 
     //RAYCAST SECTION
 
     void RaycastLoop(float rayLength, Vector2 origin, float velocity){
         Vector3 vec = Vector3.up;
         float a = 0f;
         for(int i = 0; i < 90; i ++){
             a = + 4f;
             vec = Quaternion.AngleAxis(a, Vector3.forward) * vec;
             hitInfo = Raycaster(rayLength, vec, origin, velocity);
         }
     }
 
     RaycastHit Raycaster(float rayLength, Vector2 dir, Vector2 origin, float velocity){
         RaycastHit hitIn;
         float distance = box.height / 2 + (walled? margin : Mathf.Abs(velocity * Time.deltaTime));
         Vector3 direction = velocity > 0? -dir * 20 : dir * 20;
         Debug.DrawRay(origin, direction, Color.red);
         Physics.Raycast(origin, direction, out hitIn, rayLength * 20, 1 << LayerMask.NameToLayer("NormalCollisions"));
         return hitIn;
     }
 
     void DebugLogger(){
         Debug.Log("Mouse position: " + mouseHit);
         Debug.Log("Unit position: " + unitPos);
         Debug.Log("Mouse ray layer: " + hit.collider);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Alternative to ScreenPointToRay needed 1 Answer

Detect Mouse in right side or left side For Player? 2 Answers

Camera to aim at raycast 1 Answer

Only show GameObject if at least 1 pixel is rendered 0 Answers

Follow mouse cursor (with Y = 0)? 1 Answer


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