Search Unity

Question HELP!!

Discussion in 'Scripting' started by artianrika, May 7, 2021.

  1. artianrika

    artianrika

    Joined:
    Apr 24, 2021
    Posts:
    1
    Hello,

    After so many online unity beginner courses, I finally started my first game.
    The game is simple you need to jump the ball upper and upper (like doodle jump) but the ball goes up only if you drag it. When I start the game I drag the ball and everything works but when I arrive at the first platform (ball holder) I can't drag it anymore. I tried on collision enter to make the platform trigger when the ball is there but it was not a great idea, I tried to Destroy the platform too, but the ball didn't stay on the same position(because kinematic, it went side or down without gravity).

    Please If you know a solution or only a small hint Sincerely tell me, I will be very happy if someone can help me!

    I will post screenshots and screen Record!

    All the best!


    Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Ball : MonoBehaviour {

    private bool isPressed = false;
    private Rigidbody2D rb;
    public float releaseTime = .15f;
    public Rigidbody2D hook;
    public float maxDragDistance = 2f;
    public GameObject holder;
    public PolygonCollider2D holderCollider;


    void Start(){
    rb = GetComponent<Rigidbody2D>();
    rb.isKinematic = true;
    }

    void Update(){
    if(isPressed){
    Vector2 mousePos;
    mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    if(Vector3.Distance(mousePos, hook.position) > maxDragDistance)
    {
    rb.position = hook.position + (mousePos - hook.position).normalized * maxDragDistance;
    }

    else
    {
    rb.position = mousePos;
    }
    }
    }

    void OnMouseDown(){
    isPressed = true;
    rb.isKinematic = true;
    }

    void OnMouseUp(){
    isPressed = false;
    rb.isKinematic = false;

    StartCoroutine(Release());
    }


    IEnumerator Release(){
    yield return new WaitForSeconds(releaseTime);

    GetComponent<SpringJoint2D>().enabled = false;
    this.enabled = false;
    }

    void OnCollisionEnter2D(Collision2D col){


    if(col.gameObject.tag == "Holder")
    {
    StartCoroutine(NextRelease());
    }

    }

    // HERE IS THE PROBLEM, I HOPE YOU CAN HELP ME!
    IEnumerator NextRelease(){
    yield return new WaitForSeconds(3);

    rb.isKinematic = true;
    OnMouseDown();
    OnMouseUp();

    }
    }
     

    Attached Files:

  2. Kaemcosi

    Kaemcosi

    Joined:
    Jan 4, 2021
    Posts:
    1
    On line 55 of Ball.cs you're disabling the script for some reason. Since you never enable it again anywhere, it won't do anything anymore.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Welcome! We all want you to succeed. However...

    I see this got overlooked and is a few weeks old.

    For one, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    For two, don't carpet-bomb us with the entire script. Do some research yourself to narrow it down.

    Here's some guidance: how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Beyond that, the go-to easy way to gain more insight into your problem is by liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494