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

Cancel action, like for example throwing a gameobject

Discussion in 'Scripting' started by canadienwolf, Feb 10, 2020.

  1. canadienwolf

    canadienwolf

    Joined:
    Oct 4, 2018
    Posts:
    4
    How do I make a script that cancels out another action?

    I want to cancel out the action from the mouse click "Fire1" with the mouse click "Fire2". That means that it should not let go of the box or give it any force so that it flies off.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6.  
    7.  
    8. public class PickupSystem : MonoBehaviour
    9. {
    10.    
    11.    
    12.    
    13.     //public
    14.     public Transform heldItemTransform;
    15.     public GameObject temparent;
    16.     public GameObject pickableObject;
    17.     public Vector3 puloc;
    18.  
    19.    
    20.    
    21.     //private
    22.     private GameObject _player;
    23.     public bool _pickedUp = false;
    24.     private float power;
    25.  
    26.     private void Awake()
    27.     {
    28.         _player = gameObject;
    29.     }
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.        
    35.     }
    36.  
    37.     // Update is called once per fr%%ame
    38.     void Update()
    39.     {
    40.  
    41.        
    42.         if (Input.GetKeyDown(KeyCode.E))
    43.         {
    44.             _pickedUp = !_pickedUp;
    45.  
    46.             if (_pickedUp)
    47.             {
    48.                
    49.                 pickableObject.transform.parent = heldItemTransform.transform;
    50.                 pickableObject.transform.position = heldItemTransform.transform.position;
    51.                 pickableObject.GetComponent<Rigidbody>().isKinematic = true;
    52.                 pickableObject.transform.localPosition = new Vector3(0, 0, 0);
    53.                 pickableObject.transform.localRotation = Quaternion.identity;
    54.                 PickbleColliders(false);
    55.  
    56.  
    57.                 print("Picked item up");
    58.                
    59.                 _pickedUp = true;
    60.             }
    61.             else if(_pickedUp == false)
    62.             {
    63.                
    64.                 pickableObject.transform.parent = null;
    65.                 pickableObject.GetComponent<Rigidbody>().isKinematic = false;
    66.                 PickbleColliders(true);
    67.  
    68.                 print("Sindrus 10 000");
    69.                
    70.                 _pickedUp = false;
    71.             }
    72.            
    73.         }
    74.        
    75.        
    76.         if (Input.GetButton("Fire1"))
    77.         {
    78.             power += Time.deltaTime * 100f;
    79.             print(power);
    80.         }
    81.  
    82.         if (Input.GetButtonUp("Fire1"))
    83.         {
    84.             pickableObject.transform.parent = null;
    85.             pickableObject.GetComponent<Rigidbody>().isKinematic = false;
    86.             pickableObject.GetComponent<Rigidbody>().AddForce(heldItemTransform.forward * power, ForceMode.Impulse);
    87.             PickbleColliders(true);
    88.             pickableObject = null;
    89.             power = 0;
    90.            
    91.             _pickedUp = false;
    92.         }
    93.  
    94.         if (Input.GetButtonDown("Fire2"))
    95.         {
    96.            
    97.             power = 0f;
    98.             print(power);
    99.          
    100.         }
    101.        
    102.        
    103.     }
    104.    
    105.     void OnCancel(BaseEventData eventData)
    106.     {
    107.        
    108.     }
    109.    
    110.    
    111.  
    112.     private void OnTriggerEnter(Collider Pickable)
    113.     {
    114.         if (Pickable.CompareTag("Pickable"))
    115.         {
    116.             pickableObject = Pickable.gameObject;
    117.         }
    118.     }
    119.  
    120.     private void OnTriggerExit(Collider other)
    121.     {
    122.         if (!_pickedUp)
    123.         {
    124.             pickableObject = null;
    125.         }
    126.     }
    127.  
    128.     void PickbleColliders(bool idx)
    129.     {
    130.         if (pickableObject != null)
    131.         {
    132.             foreach (Collider c in pickableObject.GetComponents<Collider>())
    133.             {
    134.                 c.enabled = idx;
    135.             }
    136.         }
    137.         void OnCancel(BaseEventData eventData);
    138.     }
    139. }
    I found this link, but I could not understand how to set it up correctly.
    https://forum.unity.com/threads/button-onclick-oncancel-action.268371/

    Do I need to make an own script with just the code in that link? The code I am referring to on that page is the last comment on it.
    Picture under:
    upload_2020-2-10_11-33-10.png
     

    Attached Files:

  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    what you posted seems like part of the UI Buttons OnClick event, didn't look into it much, not related.

    all you gotta do is this:
    1) let's keep track of a bool, call it "cancelThrow", default it to false
    2) Inside the GetButton block do a check for Fire2, if it is pressed set "cancelThrow" to true (at this point you can also back out of any throwing animation or UI you have)
    3) Inside the GetButtonUp block, first thing you do is check if "cancelThrow" is true, if it is, set it back to false and return.

    This will work for you script, but if you want to develop this thing further I suggest a more advanced system, there are more then enough tutorials on how to do this stuff in unity, just keep out the newbie hard coded tutorials (they're good for getting you comfortable with scripting, but that's as far as they should go imo)
     
    canadienwolf likes this.
  3. canadienwolf

    canadienwolf

    Joined:
    Oct 4, 2018
    Posts:
    4
    Thank you so much, it fixed my problem :)