Search Unity

Rubys adventure, weird bug

Discussion in 'Getting Started' started by JohanAhlqvist, Jul 3, 2019.

  1. JohanAhlqvist

    JohanAhlqvist

    Joined:
    Jun 28, 2019
    Posts:
    3
    Hello everyone! I posted this in the wrong part of the forum I think so Im reposting here since it seems to be the right place. Im just getting started with unity and C# and I am following the rubys adventure tutorial right now. But I have gotten a weird bug and Im not certain if it is the script(which it shouldnt be since I just wrote what is in the guide) or Unity it self.

    I am at the part where you start scripting the first enemy. When I start the game and my main character collides with the enemy character the controls stop working for the character and it sort of floats away to the side when I push any of the arrowkeys. It always floats away from the enemy character.

    Is it a bug in the collision detecting system or is it more probable that it is a bug in my scripts?

    I am using Unity 2019.1.8f1

    Below is the script for the main character

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class RubyController : MonoBehaviour
    8. {
    9.     public int maxHealth = 5;
    10.     public float timeInvincible = 2.0f;
    11.  
    12.     public int health { get { return currentHealth; } }
    13.     int currentHealth;
    14.     bool isInvincible;
    15.     float invincibleTimer;
    16.     public float characterSpeed = 3.0f;
    17.  
    18.  
    19.     Rigidbody2D rigidbody2d;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rigidbody2d = GetComponent<Rigidbody2D>();
    25.         currentHealth = maxHealth;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         float horizontal = Input.GetAxis("Horizontal");
    32.         float vertical = Input.GetAxis("Vertical");
    33.         Vector2 position = rigidbody2d.position;
    34.         position.x = position.x + characterSpeed * horizontal * Time.deltaTime;
    35.         position.y = position.y + characterSpeed * vertical * Time.deltaTime;
    36.         transform.position = position;
    37.  
    38.         rigidbody2d.MovePosition(position);
    39.  
    40.         if (isInvincible)
    41.         {
    42.             invincibleTimer -= Time.deltaTime;
    43.             if (invincibleTimer < 0)
    44.                 isInvincible = false;
    45.         }
    46.     }
    47.  
    48.     public void ChangeHealth(int amount)
    49.     {
    50.         if (amount < 0)
    51.         {
    52.             if (isInvincible)
    53.                 return;
    54.  
    55.             isInvincible = true;
    56.             invincibleTimer = timeInvincible;
    57.         }
    58.  
    59.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    60.  
    61.         Debug.Log(currentHealth + "/" + maxHealth);
    62.     }
    63. }
    64.  
    and below is the script for the enemy

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class EnemyController : MonoBehaviour
    8. {
    9.     public float enemySpeed = 3.0f;
    10.     public bool vertical;
    11.     public float changeTime = 3.0f;
    12.  
    13.     Rigidbody2D rigidbody2D;
    14.     float timer;
    15.     int direction = 1;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         rigidbody2D = GetComponent<Rigidbody2D>();
    21.         timer = changeTime;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         timer -= Time.deltaTime;
    28.  
    29.         if (timer < 0)
    30.         {
    31.             direction = -direction;
    32.             timer = changeTime;
    33.         }
    34.  
    35.         Vector2 position = rigidbody2D.position;
    36.        
    37.         if (vertical)
    38.         {
    39.             position.y = position.y + Time.deltaTime * enemySpeed * direction;
    40.         }
    41.  
    42.         else
    43.         {
    44.             position.x = position.x + Time.deltaTime * enemySpeed * direction;
    45.         }
    46.  
    47.         rigidbody2D.MovePosition(position);
    48.     }
    49.  
    50.     void OnCollisionEnter2D(Collision2D other)
    51.     {
    52.         RubyController player = other.gameObject.GetComponent<RubyController >();
    53.  
    54.         if (player != null)
    55.         {
    56.             player.ChangeHealth(-1);
    57.         }
    58.     }
    59. }
    60.  
    61.