Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BastingQuill · Nov 18, 2020 at 01:12 AM · instantiateselectiongrabmultiple objectshighlighting

Need help to grab 1 object at a time from multiple objects available

Hello, I am trying to do a minigame where you need to steal objects from NPC's. I am using the new Unity input system and currently testing with a PS4 controller. You basically have 3 buttons you can use to steal for example: Square, LT and RT. (You hold to grab and release to let go). My Goal is to Instantiate the Object grabbed to the corresponding position, according to the Input.

The boxes are items you can grab the 1st picture is what i would like to do. I already have the controller part working. I just cant seem to get only 1 item at a time..

alt text

Square = C Position | LT = D Position | RT = E Position

However when i Press any of these buttons all objects go to the input position, to the point you can have unlimited Objects in one same position. I only want 1 object to be in each position. If the position is occupied then you cant grab any more objects unless you drop the item in hand.

I was thinking in highlighting the nearest object to the player and add only that one (if (itemHighlighted == true) { add obj to location }, if(locationD == full) { cant grab object })... However i cant highlight only 1 item... like in the picture below...

alt text

Here is the script i have for now, please help! I would really appreciate it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SelectionManagerScript : MonoBehaviour
 {
     [SerializeField] private Material Highlight;
     [SerializeField] private Material NonStealableItemMaterial;
     [SerializeField] private Material StealableItemMaterial;
 
     GameObject ClosestItem;
 
     public bool NeutralItem;
     public bool canSteal;
 
     //TO DO:
     //Highlight only the closest item to the player
     //Grab only 1 item at a time
     //Place item grabbed according to input location
 
     public GameObject FindClosestItem()
     {
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag("Item");
         GameObject closest = null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
         foreach (GameObject go in gos)
         {
             Vector3 diff = go.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < distance)
             {
                 closest = go;
                 Debug.Log(closest);
                 distance = curDistance;
                 Debug.Log(distance);
             }
         }
         return closest;
     }
 
     private void OnTriggerEnter(Collider other)
     {
         canSteal = true;
 
         ClosestItem = FindClosestItem();
         ShadeMaterial(other);
     }
 
     /*private void OnTriggerStay(Collider other)
     {
         canSteal = true;
 
         ClosestItem = FindClosestItem();
         ShadeMaterial(other);
     }*/
 
     private void OnTriggerExit(Collider other)
     {
         canSteal = false;
 
         ClosestItem = FindClosestItem();
         DefaultMaterial(other);
     }
 
     void ShadeMaterial(Collider item) 
     {
          var selectionRenderer = ClosestItem.GetComponent<Renderer>();
          if (selectionRenderer != null)
          {
              selectionRenderer.material = Highlight; //highlight the obj
          }
     }
 
     void DefaultMaterial(Collider item) 
     {
         bool stealObj;
         stealObj = item.GetComponent<ItemCollider>().StealableItem;
         if (stealObj == true)
         {
             var selectionRenderer = ClosestItem.GetComponent<Renderer>();
             if (selectionRenderer != null)
             {
                 selectionRenderer.material = StealableItemMaterial;
             }
         }
 
         if (stealObj == false)
         {
             var selectionRenderer = ClosestItem.GetComponent<Renderer>();
             if (selectionRenderer != null)
             {
                 selectionRenderer.material = NonStealableItemMaterial;
             }
         }
     }
 }

question5.png (121.0 kB)
question3.png (128.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If instantiate prefab is selected how can i change the color? 1 Answer

Spawn and destroy gameObject by tag 0 Answers

Spawn a random object on more than one position 1 Answer

Gameobject array 0 Answers

Please Explain the Code 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges