- Home /
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);
}
}
Your answer
Follow this Question
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