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

enemy jitter when colliding with player

Discussion in '2D' started by MaudesMeow, Sep 2, 2022.

  1. MaudesMeow

    MaudesMeow

    Joined:
    Jul 25, 2021
    Posts:
    3
    Hello!

    New to Unity in that I'm finally making small games with having to look at tutorials constantly! So proud of that, but currently stuck with this issue that I can't seem to wrap my head around. I have it so when my player walks into an enemy collider, the player stops and the enemy walks over to the player to begin battle. The issue is that when the enemy gets to the player it starts jittering. and even when I set moveEnemy = false; (if directionToPlayer.x = 0 or something) it still won't stop. Any help would be greatly appreciated!

     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    What's your player movement code?
    Also, I suggest using code tags
     
  3. MaudesMeow

    MaudesMeow

    Joined:
    Jul 25, 2021
    Posts:
    3
    hello!

    I know... I need to get better about use code tags. I always think about them as I'm writing a script and am like "ill set it after this!" and then I forget, it's a bad habit I'm working on while programming.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.  
    9.     private Vector2 movement;
    10.     private Rigidbody2D playerRB;
    11.  
    12.     public float speed = 5f;
    13.  
    14.     public Animator animator;
    15.     //public Animator transitionLevel;
    16.  
    17.     public float transistionTime = 10f;
    18.  
    19.     private bool canMove = true;
    20.  
    21.  
    22.    
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         canMove = true;
    27.      
    28.         animator.SetBool("isActive", true);
    29.         playerRB = GetComponent<Rigidbody2D>();
    30.        
    31.         animator.SetFloat("LastVertical", -1);
    32.        
    33.        
    34.     }
    35.  
    36.     // Update is called once per frame
    37.     void Update()
    38.     {
    39.         if (canMove)
    40.         {
    41.             movement.x = Input.GetAxisRaw("Horizontal");
    42.             movement.y = Input.GetAxisRaw("Vertical");
    43.  
    44.             animator.SetFloat("Horizontal", movement.x);
    45.             animator.SetFloat("Vertical", movement.y);
    46.             animator.SetFloat("Speed", movement.sqrMagnitude);
    47.  
    48.             if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
    49.             {
    50.                 animator.SetFloat("LastHorizontal", Input.GetAxisRaw("Horizontal"));
    51.                 animator.SetFloat("LastVertical", Input.GetAxisRaw("Vertical"));
    52.             }
    53.  
    54.  
    55.         } else
    56.         {
    57.            
    58.             animator.SetFloat("LastHorizontal", 0);
    59.             animator.SetFloat("LastVertical", -1);
    60.             animator.SetFloat("Speed", 0);
    61.            
    62.         }
    63.  
    64.  
    65.  
    66.     }
    67.  
    68.     private void FixedUpdate()
    69.     {
    70.      
    71.  
    72.        
    73.         if (canMove)
    74.         {
    75.             Vector2 moveHorizontal = transform.right * movement.x;
    76.             Vector2 moveVertical = transform.up * movement.y;
    77.             Vector2 velocity = ((moveHorizontal + moveVertical).normalized * speed);
    78.             playerRB.MovePosition(playerRB.position + velocity * Time.fixedDeltaTime);
    79.         }
    80.      
    81.     }
    82.  
    83.  
    84.     private IEnumerator OnTriggerEnter2D(Collider2D collision)
    85.     {
    86.         if (collision.gameObject.name.Equals("LevelEnemy"))
    87.         {
    88.             canMove = false;
    89.             yield return new WaitForSeconds(.5f);
    90.             StartCoroutine(LoadLevel("FightScene"));  
    91.         }
    92.     }
    93.  
    94.     IEnumerator LoadLevel(string LevelName)
    95.     {
    96.         //transitionLevel.SetTrigger("Start");
    97.  
    98.         yield return new WaitForSeconds(transistionTime);
    99.  
    100.         LevelLoader.levelToLoad = "FightScene";
    101.         LevelLoader.timeToTransistion = true;
    102.  
    103.     }
    104.  
    105.  
    106.  
    107.  
    108.     public void setMovementBool(bool isItTrue)
    109.     {
    110.         canMove = isItTrue;
    111.     }
    112. }
    113.  
     
  4. MaudesMeow

    MaudesMeow

    Joined:
    Jul 25, 2021
    Posts:
    3
    I figured it out! Since I have the player object stand still once seen, I use the lerp function. Let me know if there's a better solution! but so far happy with this!
     
    DevDunk likes this.