Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player glitches out if in middle of mouse

Discussion in 'Scripting' started by S3Critsss, Jun 17, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I need help with fixing this bug i have. i have my player control script which makes the player move toward the mouse position but if i don't move the mouse and it reaches the mouse position it messes up the rotation instead of just staying there in the direction it faces.

    This is what i mean. you can't see the picture clearly but the player's head is on both sides:
    upload_2020-6-17_19-49-51.png

    this is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControls : MonoBehaviour
    6. {
    7.  
    8.     public GameObject pauseMenu;
    9.     public GameObject SpeedBoostText;
    10.     public GameObject SpeedBoostSFX;
    11.  
    12.     public float moveSpeed = 5f;
    13.  
    14.     bool isSpeeding = false;
    15.     float TimeBetweenSpeedBoost = 20f;
    16.     float TimeTillBoostEnds = 5f;
    17.     float reducingTime = 1.0f;
    18.  
    19.     Rigidbody2D rb;
    20.  
    21.     CameraShake cameraShake;
    22.  
    23.     private void Start()
    24.     {
    25.         rb = this.GetComponent<Rigidbody2D>();
    26.     }
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         //Pausing
    31.         if (Input.GetKeyDown(KeyCode.Escape))
    32.         {
    33.             Time.timeScale = 0f;
    34.             Camera.main.GetComponent<AudioSource>().Stop();
    35.             pauseMenu.SetActive(true);
    36.             cameraShake.shouldShake = false;
    37.         }
    38.  
    39.         //Player Faces Mouse
    40.         Vector3 mousePos = Input.mousePosition;
    41.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    42.         Vector2 direction = new Vector2(mousePos.x - transform.position.x,mousePos.y - transform.position.y);
    43.         transform.up = direction;
    44.  
    45.         //Player Moves Towards Mouse
    46.         direction.Normalize();
    47.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    48.  
    49.         //SpeedBoost
    50.         TimeBetweenSpeedBoost -= reducingTime * Time.deltaTime;
    51.         if (TimeBetweenSpeedBoost <= 0f)
    52.         {
    53.             isSpeeding = true;
    54.             if (isSpeeding)
    55.             {
    56.                 Instantiate(SpeedBoostSFX, transform.position, transform.rotation);
    57.                 SpeedBoostText.SetActive(true);
    58.                 moveSpeed = 14f;
    59.                 TimeTillBoostEnds -= reducingTime * Time.deltaTime;
    60.                 if (TimeTillBoostEnds <= 0f)
    61.                 {
    62.                     isSpeeding = false;
    63.                     SpeedBoostText.SetActive(false);
    64.                     moveSpeed = 7f;
    65.                     TimeBetweenSpeedBoost = 20f;
    66.                     TimeTillBoostEnds = 5f;
    67.                 }
    68.             }
    69.         }
    70.     }
    71. }
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,003
    First:
    Do not use Input in the FixedUpdate method (the old one). You could lose input events this way, depending if your fixed time scale is default or not.
    Cache your Camera.main in a variable like you do with the RigidBody. (performance optimization, do whenever you feel like it)

    Second:
    I don't see anywhere in your code where you would exclude the case when the player is already nearby or on the top of the mouse.
    You need are calculating your distance on the 42nd line if it is sufficiently small (you need to define what does this mean), you need to bail out instead of moving the player.
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I'm sorry but i don't understand how i would calculate the distance in the 42nd line and bail out.

    I tried this but it doesn't work:
    Code (CSharp):
    1. //Player Faces Mouse
    2.         Vector3 mousePos = Input.mousePosition;
    3.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    4.         if (transform.position != mousePos)
    5.         {
    6.             Vector2 direction = new Vector2(mousePos.x - transform.position.x,mousePos.y - transform.position.y);
    7.             transform.up = direction;
    8.  
    9.             //Player Moves Towards Mouse
    10.             direction.Normalize();
    11.             rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    12.         }