Search Unity

Would like some help

Discussion in '2D' started by Babooh, Aug 3, 2019.

  1. Babooh

    Babooh

    Joined:
    Jul 26, 2019
    Posts:
    10
    I'm really new to unity and made this "game". As you can see in this video, the red ball rotates around the blue ones at contact, and when it leaves the screen the red ball resets to it's original position. Problem is that sometimes it gets stuck on the blue circles, which is shown in the video. My question isn't why it gets stuck, because I think I know where that problem might come from. I'm just wondering why it only gets stuck sometimes. Does it have to do with framerate maybe?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Without seeing how you coded it, we would just be taking guesses.

    You can paste your code here with code tags using the Code buttons in the toolbar. See the link in my signature.
     
  3. Babooh

    Babooh

    Joined:
    Jul 26, 2019
    Posts:
    10
    Reason I didn't post my code was because I thought it would be something simple that doesn't have to do with any code.

    Anyway here's my code for the player/red ball:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerScript : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public Rigidbody2D rigidbody;
    10.     Vector3 direction = new Vector3(0,1,0);
    11.     Vector3 targetPosition;
    12.     public bool isMoving;
    13.  
    14.     void Start()
    15.     {
    16.         isMoving = true;
    17.         rigidbody = GetComponent<Rigidbody2D>();
    18.         QualitySettings.vSyncCount = 0;
    19.         Application.targetFrameRate = 60;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             Debug.Log("space");
    27.             isMoving = true;
    28.         }
    29.  
    30.         if (isMoving)
    31.         {
    32.             direction.Normalize();
    33.             Vector3 position = transform.position;
    34.             rigidbody.MovePosition(position + direction * speed * Time.deltaTime);
    35.         }
    36.     }
    37.  
    38.     public void Rotate(Vector3 originPosition, float rotatingSpeed)
    39.     {
    40.         if (!isMoving)
    41.         {
    42.             rigidbody.velocity = Vector3.zero;
    43.             targetPosition = originPosition;
    44.             direction = transform.position - targetPosition;
    45.             transform.RotateAround(targetPosition, new Vector3(0, 0, 1), 1 * rotatingSpeed * Time.deltaTime);
    46.         }
    47.      
    48.     }
    49.  
    50.     void OnBecameInvisible()
    51.     {
    52.         Destroy(gameObject);
    53.         SceneManager.LoadScene("SampleScene");
    54.     }
    Here's the code for the blue balls:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotatingCircle : MonoBehaviour
    6. {
    7.     public float rotatingSpeed;
    8.     PlayerScript player;
    9.  
    10.     void Start()
    11.     {
    12.         player = FindObjectOfType<PlayerScript>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Vector2 position = transform.position;
    18.         transform.RotateAround(position, new Vector3(0,0,1), 1 * rotatingSpeed * Time.deltaTime);
    19.     }
    20.  
    21.     void OnTriggerStay2D(Collider2D other)
    22.     {
    23.         player.Rotate(transform.position, rotatingSpeed);
    24.     }
    25.  
    26.     void OnCollisionEnter2D(Collision2D other)
    27.     {
    28.         player.isMoving = false;
    29.         Debug.Log("CollisionEnter2D");
    30.     }
    31. }
    32.  
    Thanks!
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    So it looks like your blue ball OnTriggerStay function stops being called, which stops your player from rotating around it. That could be caused by the physics system separating the objects after a collision, which moves it out of range of the trigger. You may be able to solve this by increasing the size of the trigger.