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

2D Game Hiting ball bug

Discussion in '2D' started by hamzaasim95, Mar 31, 2020.

  1. hamzaasim95

    hamzaasim95

    Joined:
    Nov 23, 2019
    Posts:
    2


    How to fix my charcter doesnt move if ball bounces back from wall

    upload_2020-3-31_11-17-44.png

    Player script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     private Rigidbody2D myRigidbody;
    9.     private Vector3 change;
    10.     private Animator animator;
    11.  
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         animator = GetComponent<Animator>();
    17.         myRigidbody = GetComponent<Rigidbody2D>();
    18.  
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.         change = Vector3.zero;
    24.         change.x = Input.GetAxisRaw("Horizontal");
    25.         // A & D
    26.         change.y = Input.GetAxisRaw("Vertical");
    27.  
    28.         // W & S
    29.         UpdateAnimationAndMove();
    30.  
    31.     }
    32.  
    33.     void UpdateAnimationAndMove()
    34.     {
    35.         if (change != Vector3.zero)
    36.         {
    37.             MoveCharacter();
    38.             animator.SetFloat("moveX", change.x);
    39.             animator.SetFloat("moveY", change.y);
    40.             animator.SetBool("moving", true);
    41.         }
    42.  
    43.         else
    44.         {
    45.             animator.SetBool("moving", false);
    46.         }
    47.  
    48.         }
    49.  
    50.     void MoveCharacter()
    51.     {
    52.         myRigidbody.MovePosition(
    53.             transform.position + change * speed * Time.deltaTime
    54.             );
    55.     }
    56. }
    57.  
     
  2. hamzaasim95

    hamzaasim95

    Joined:
    Nov 23, 2019
    Posts:
    2
    How to stop my character from moving after getting hit by ball bounced by wall