Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How do I assign a transform automatically in game?

Discussion in 'Scripting' started by guyanermanator, May 28, 2019.

  1. guyanermanator

    guyanermanator

    Joined:
    Mar 17, 2018
    Posts:
    13
    So I have some objects I can pick up and a weapon system. The thing is that I want the pick up script to not be active when the weapons are because it looks silly when you are trying to shoot something but you pick it up at the same time.


    Here is my script for picking objects up.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.     public Transform theDest;
    8.     void OnMouseDown()
    9.     {
    10.         GetComponent<Rigidbody>().useGravity = false;
    11.         this.transform.position = theDest.position;
    12.         this.transform.parent = GameObject.Find("destination").transform;
    13.     }
    14.  
    15.     private void OnMouseUp()
    16.     {
    17.         this.transform.parent = null;
    18.         GetComponent<Rigidbody>().useGravity = true;
    19.     }
    20.  
    21.    
    22.  
    23.  
    24.  
    25. }
    26.  
    The pick up script is attached to all objects that are able to be lifted.

    Thank you for your helping. I am still learning. Currently, I have a game some visual scripting done to create and destroy the "destination" game object transform. The problem is that once it is created again it does not automatically assign itself as the transform again. I would like to keep the visual scripting I already did with GameCreator implemented but if not that's okay too. I have the ON and OFF part situated it's just the transform bit that I find tricky.
     
  2. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    Well,
    Make a class (ScriptableObject class works well) and write the informations you need like: canBePickedUp, weaponType, Range, AttackSpeed, etc..
    https://docs.unity3d.com/ScriptReference/ScriptableObject.html

    Now, the player need to pickup weapons, not the weapon need to lift up by itself to the player hand. You need to make a class with only pickup algorithm and checking if you have already a weapon equiped.
    Code (CSharp):
    1. if (player_want_to_pickUp_object)
    2.   if (object.canBePickedUp == true)
    3.     if (player has nothing in hand)
    4.       PickUpWeapon();
    5. if (player_want_to_drop_object)
    6.    if (player has something equiped in hand)
    7.      DropWeapon();