Search Unity

An object on an object

Discussion in 'Scripting' started by takixx, Aug 21, 2018.

  1. takixx

    takixx

    Joined:
    Apr 10, 2017
    Posts:
    55
    I have one ball.

    If you stay on ball "cube" for longer than 2 seconds, restart ball.

    Because user has the right to 2 shoot to 1 Ball.

    22.png 23.png 24.png
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    This is good to know. And the screenshots look nice too.

    But what is your question?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Check if the rigidbody's velocity is below a certain threshold, and if it is, increase a timer count. Once the timer reaches 2 seconds, reset the ball.
     
  4. takixx

    takixx

    Joined:
    Apr 10, 2017
    Posts:
    55
    PLEASE SHOW ME :)


    RESPAWN CODE

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class Respawn : MonoBehaviour {
    8.     private Vector2 StartPoint;
    9.     public Rigidbody2D rb;
    10.     private Vector2 StartPointRotation;
    11.     Vector2 startPos, endPos;
    12.     private GameObject ball;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         //StartPoint = this.transform.position;
    17.         StartPoint = transform.position;
    18.         Rigidbody2D rb = GetComponent<Rigidbody2D>();
    19.         //rb.gravityScale = 0f;
    20.  
    21.     }
    22.  
    23.     void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         if (collision.gameObject.CompareTag("Respawn"))
    26.         {
    27.  
    28.  
    29.             //SceneManager.LoadScene("SampleScene");
    30.             //transform.position = StartPoint;
    31.  
    32.             //Reset();
    33.             //Save();
    34.             //this.transform.position = StartPoint + new Vector3(Random.Range(-7f, 1f), -7f, 1f);
    35.  
    36.             //GetComponent<Rigidbody2D>().position = Vector2.zero;
    37.             Rigidbody2D rb = GetComponent<Rigidbody2D>();
    38.             rb.velocity = Vector2.zero;
    39.             rb.gravityScale = 0f;
    40.             rb.angularVelocity = 0f;
    41.  
    42.             //collision.attachedRigidbody.isKinematic = true;
    43.             //collision.attachedRigidbody.gravityScale = -100;
    44.             //collision.attachedRigidbody.gravityScale = 0f;
    45.             //Time.timeScale = 0;
    46.             transform.position = StartPoint;
    47.             //Time.timeScale = 1;
    48.             //rb.velocity = new Vector3(-7, 1, -2);
    49.            
    50.         }
    51.         if (collision.gameObject.CompareTag("basket"))
    52.         {
    53.             Rigidbody2D rb = GetComponent<Rigidbody2D>();
    54.             rb.velocity = Vector2.zero;
    55.             rb.gravityScale = 0f;
    56.             rb.angularVelocity = 0f;
    57.             transform.position = StartPoint;
    58.         }
    59.  
    60.  
    61.  
    62.     }
    63.  
    64.     void Resume()
    65.     {
    66.         Time.timeScale = 1;
    67.  
    68.     }
    69.  
    70.    
    71. }
    72.  

    BALL CODE


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class trajectoryScript : MonoBehaviour {
    6.  
    7.     public Sprite dotSprite;                    //All of the dots will become the sprite assigned to this if this has a sprite assigned to it and changeSpriteAfterStart is true
    8.     public bool changeSpriteAfterStart;            //When enabled, you will be able to change the above in the update loop. (it's less efficient)
    9.     public float initialDotSize;                //The intial size of the trajectoryDots gameobject
    10.     public int numberOfDots;                    //The number of points representing the trajectory
    11.     public float dotSeparation;                    //The space between the points representing the trajectory
    12.     public float dotShift;                        //How far the first dot is from the "ball"
    13.     public float idleTime;                        //How long the player has to be inactive for the Help Gesture to begin animating
    14.     private GameObject trajectoryDots;            //The parent of all the points representing the trajectory
    15.     private GameObject ball;                    //The projectile the player will be shooting
    16.     private Rigidbody2D ballRB;                    //The Rigidbody2D attached to the projectile the player will be shooting
    17.     private Vector3 ballPos;                    //Position of the ball
    18.     private Vector3 fingerPos;                    //Position of the pressed down finger/cursor on the screen
    19.     private Vector3 ballFingerDiff;                //The distance between where the finger/cursor is and where the "ball" is when screen is being pressed
    20.     private Vector2 shotForce;                    //How much velocity will be applied to the ball
    21.     private float x1, y1;                        //X and Y position which will be applied to each point of the trajectory
    22.     private GameObject helpGesture;                //The Help Gesture which will become active after a period of inactivity
    23.     private float idleTimer = 7f;                //How long the initial inactivity period will need to be before the Help Gesture shows up
    24.     private bool ballIsClicked = false;            //If the cursor is hovering over the "Ball Click Area"
    25.     private bool ballIsClicked2 = false;        //If the finger/cursor is pressing down in the "Ball Click Area" to activate the shot
    26.     private GameObject ballClick;                //The area which the player needs to click in to activate a shot
    27.     public float shootingPowerX;                //The amount of power which can be applied in the X direction
    28.     public float shootingPowerY;                //The amount of power which can be applied in the Y direction
    29.     public bool usingHelpGesture;                //If you want to use the Help Gesture
    30.     public bool explodeEnabled;                    //If you want to do something when the projectile reaches the last point of the trajectory
    31.     public bool grabWhileMoving;                //Off means the player won't be able to shoot until the "ball" is still. On means they can stop the "ball" by clicking on it and shoot
    32.     public GameObject[] dots;                    //The array of points that make up the trajectory
    33.     public bool mask;
    34.     private BoxCollider2D[] dotColliders;
    35.  
    36.  
    37.     void Start () {
    38.  
    39.      
    40.        
    41.  
    42.  
    43.  
    44.         ballRB = GetComponent<Rigidbody2D>();
    45.         ballRB.gravityScale = 0f;
    46.         ball = gameObject;                                            //Script has to be applied to the "ball"
    47.         ballClick = GameObject.Find ("Ball Click Area");            //BALL CLICK AREA MUST HAVE THE SAME NAME IN HIERARCHY AS IT DOES HERE OTHERWISE SHOOTING WON'T BE POSSIBLE AND OTHER ERRORS MAY OCCUR
    48.         trajectoryDots = GameObject.Find ("Trajectory Dots");        //TRAJECTORY DOTS MUST HAVE THE SAME NAME IN HIERARCHY AS IT DOES HERE
    49.         if (usingHelpGesture == true) {                                //If you're using the Help Gesture
    50.             helpGesture = GameObject.Find ("Help Gesture");            //HELP GESTURE MUST HAVE THE SAME NAME IN HIERARCHY AS IT DOES HERE IF usingHelpGesture is true
    51.         }
    52.         ballRB = GetComponent<Rigidbody2D> ();                        //"Ball"'s Rigidbody2D is applied to ballRB
    53.  
    54.         trajectoryDots.transform.localScale = new Vector3 (initialDotSize, initialDotSize, trajectoryDots.transform.localScale.z); //Initial size of trajectoryDots is applied
    55.  
    56.         for (int k = 0; k < 40; k++) {
    57.             dots [k] = GameObject.Find ("Dot (" + k + ")");            //All points are applied to the corresponding position in the dots array
    58.             if (dotSprite != null) {                                //If a sprite is applied to dotSprite
    59.                 dots [k].GetComponent<SpriteRenderer> ().sprite = dotSprite;    //All points will have that sprite applied
    60.             }
    61.         }
    62.         for (int k = numberOfDots; k < 40; k++) {                    //If the number of points being used is less than 40, the maximum...
    63.             GameObject.Find ("Dot (" + k + ")").SetActive (false);    //They will be hidden
    64.         }
    65.         trajectoryDots.SetActive (false);                            //Trajectory initialization complete, the trajectory is hidden
    66.    
    67.  
    68.         }
    69.    
    70.  
    71.        
    72.  
    73.     void Update () {
    74.        
    75.  
    76.  
    77.         if (numberOfDots > 40) {
    78.             numberOfDots = 40;
    79.         }
    80.  
    81.         if (usingHelpGesture == true) {                                //If you're using the Help Gesture...
    82.             helpGesture.transform.position = new Vector3 (ballPos.x, ballPos.y, ballPos.z);    //It will have the same position as the "ball"
    83.         }
    84.  
    85.         RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);    //Used to determine if the finger/cursor is on the Ball Click Area
    86.  
    87.         if (hit.collider != null && ballIsClicked2 == false) {                    //If the the location of the cursor is over anything and the player hasn't activated the shot already... (This has to be done first since something has to be applied to the hit.collider before asking what the name is)
    88.             if (hit.collider.gameObject.name == ballClick.gameObject.name) {    //and If the name of what the cursor is overlapping is the same as the "ball"'s name...
    89.                 ballIsClicked = true;                                     //First step of activating the shot is done
    90.             } else {                                                            //If the name of what the cursor is overlapping is something other than the "ball"...
    91.                 ballIsClicked = false;                                            //Don't start activating the shot
    92.             }
    93.         } else {                                                                //If the cursor isn't overlapping anything or the shot is already activated...
    94.             ballIsClicked = false;                                                //Don't activate/reactivate the shot
    95.         }
    96.  
    97.         if (ballIsClicked2 == true) {                                            //If shot is already activated...
    98.             ballIsClicked = true;                                                //Keep ballIsClicked true for later
    99.         }
    100.  
    101.  
    102.         if ((ballRB.velocity.x * ballRB.velocity.x) + (ballRB.velocity.y * ballRB.velocity.y) <= 0.0085f) { //if the "ball" is moving really slow...
    103.             ballRB.velocity = new Vector2 (0f, 0f);
    104.              //Make the "ball" stop moving
    105.             idleTimer -= Time.deltaTime;                                        //Begin the timer for the Help Gesture
    106.            
    107.         } else {                                                                //If the "ball" is still moving fast enough...
    108.             trajectoryDots.SetActive (false);                                    //Don't allow the trajectory to be shown. (This is for if you're in the process of aiming and something causes the ball to move)
    109.         }
    110.  
    111.         if (usingHelpGesture == true && idleTimer <= 0f) {                        //If you're using the Help Gesture and the amount of time the player has been idle for has expired...
    112.             helpGesture.GetComponent<Animator> ().SetBool ("Inactive", true);    //Begin the Help animation
    113.         }
    114.    
    115.  
    116.         ballPos = ball.transform.position;
    117.                                                                       //ballPos is updated to the position of the "ball"
    118.  
    119.         if (changeSpriteAfterStart == true) {                                    //If you've allowed the sprite to be continiously changed...
    120.             for (int k = 0; k < numberOfDots; k++) {
    121.                 if (dotSprite != null) {                                        //If a sprite is applied to dotSprite
    122.                     dots [k].GetComponent<SpriteRenderer> ().sprite = dotSprite;//Change all points' sprite to the dotSprite sprite
    123.                 }
    124.             }
    125.         }
    126.  
    127.  
    128.         if ((Input.GetKey (KeyCode.Mouse0) && ballIsClicked == true) && ((ballRB.velocity.x == 0f && ballRB.velocity.y == 0f) || (grabWhileMoving == true))) {    //If player has activated a shot                                        //when you press down
    129.             ballIsClicked2 = true;                                                //Final step of activation is complete
    130.  
    131.             if (usingHelpGesture == true) {                                        //If you're using the Help Gesture...
    132.                 idleTimer = idleTime;                                            //It is reset
    133.                 helpGesture.GetComponent<Animator> ().SetBool ("Inactive", false);    //If the animation is playing, it will stop
    134.             }
    135.  
    136.             fingerPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);     //The position of your finger/cursor is found
    137.             fingerPos.z = 0;                                                    //The z position is set to 0
    138.  
    139.             if (grabWhileMoving == true) {                                        //If you've enabled shooting while the ball is moving
    140.                 ballRB.velocity = new Vector2 (0f, 0f);                            //The "ball" stops moving
    141.                 ballRB.isKinematic = true;                                        //The "ball" isn't affected by other forces (it stays in the same spot)
    142.         }
    143.  
    144.             ballFingerDiff = ballPos - fingerPos;                                //The distance between the finger/cursor and the "ball" is found
    145.            
    146.             shotForce = new Vector2 (ballFingerDiff.x * shootingPowerX, ballFingerDiff.y * shootingPowerY);    //The velocity of the shot is found
    147.  
    148.             if ((Mathf.Sqrt ((ballFingerDiff.x * ballFingerDiff.x) + (ballFingerDiff.y * ballFingerDiff.y)) > (0.4f))) { //If the distance between the finger/cursor and the "ball" is big enough...
    149.                 trajectoryDots.SetActive (true);                                //Display the trajectory
    150.             } else {
    151.                 trajectoryDots.SetActive (false);                                //Otherwise... Cancel the shot
    152.                 if (ballRB.isKinematic == true) {
    153.                     ballRB.isKinematic = false;
    154.                 }
    155.             }
    156.  
    157.             for (int k = 0; k < numberOfDots; k++) {                            //Each point of the trajectory will be given its position
    158.                 x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);    //X position for each point is found
    159.             y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y/2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (dotSeparation * k + dotShift));    //Y position for each point is found
    160.                 dots [k].transform.position = new Vector3 (x1, y1, dots [k].transform.position.z);    //Position is applied to each point
    161.             }
    162.         }
    163.  
    164.  
    165.         if (Input.GetKeyUp (KeyCode.Mouse0)) {                                //If the player lets go...
    166.        
    167.             ballIsClicked2 = false;                                            //Aiming is no longer happening
    168.  
    169.             if (trajectoryDots.activeInHierarchy) {                            //If the player was aiming...
    170.                 if(explodeEnabled == true){                                    //If the player was shooting and explodeEnabled is true...
    171.                 StartCoroutine(explode ());                                    //The "explode" coroutine will start
    172.             }
    173.             trajectoryDots.SetActive (false);                                //The trajectory will hide
    174.                 ballRB.velocity = new Vector2 (shotForce.x, shotForce.y);   //The "ball" will have its new velocity
    175.                 ballRB.gravityScale = 1f;  
    176.                 if (ballRB.isKinematic == true) {                            //If the "ball" was kinematic...
    177.                     ballRB.isKinematic = false;                                //It's no longer kinematic
    178.             }
    179.         }
    180.     }
    181. }
    182.  
    183.     public IEnumerator explode(){                                            //The explode function
    184.         yield return new WaitForSeconds (Time.fixedDeltaTime * (dotSeparation * (numberOfDots - 1f)));    //Nothing will happen until the time it takes for the projectile to reach the last point of the trajectory passes
    185.         Debug.Log ("exploded");
    186.    
    187.  
    188.     //Insert what happens when the time it takes for the projectile to reach the last point of the trajectory expires, (explodeEnabled has to be true)
    189.  
    190.     }
    191.  
    192.     public void collided(GameObject dot){
    193.  
    194.         for (int k = 0; k < numberOfDots; k++) {
    195.             if (dot.name == "Dot (" + k + ")") {
    196.                
    197.                 for (int i = k + 1; i < numberOfDots; i++) {
    198.                    
    199.                     dots [i].gameObject.GetComponent<SpriteRenderer> ().enabled = false;
    200.                 }
    201.  
    202.             }
    203.  
    204.         }
    205.     }
    206.     public void uncollided(GameObject dot){
    207.         for (int k = 0; k < numberOfDots; k++) {
    208.             if (dot.name == "Dot (" + k + ")") {
    209.  
    210.                 for (int i = k-1; i > 0; i--) {
    211.                
    212.                     if (dots [i].gameObject.GetComponent<SpriteRenderer> ().enabled == false) {
    213.                         Debug.Log ("nigggssss");
    214.                         return;
    215.                     }
    216.                 }
    217.  
    218.                 if (dots [k].gameObject.GetComponent<SpriteRenderer> ().enabled == false) {
    219.                     for (int i = k; i > 0; i--) {
    220.                        
    221.                         dots [i].gameObject.GetComponent<SpriteRenderer> ().enabled = true;
    222.  
    223.                     }
    224.  
    225.                 }
    226.             }
    227.  
    228.         }
    229.     }
    230. }
    231.  
    232.  
     
  5. takixx

    takixx

    Joined:
    Apr 10, 2017
    Posts:
    55
    brothers ?
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What part are you stuck on? You already have the idea here:
    Code (csharp):
    1.  
    2.         if ((ballRB.velocity.x * ballRB.velocity.x) + (ballRB.velocity.y * ballRB.velocity.y) <= 0.0085f) { //if the "ball" is moving really slow...
    3.             ballRB.velocity = new Vector2 (0f, 0f);
    4.              //Make the "ball" stop moving
    5.             idleTimer -= Time.deltaTime;                                        //Begin the timer for the Help Gesture
    6.           ...
    7.