Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

In need of assistence

Discussion in 'Scripting' started by mikuru15, Nov 22, 2014.

  1. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    So I been trying to make a simple oculus rift game in unity where you walk around finding objects and collecting them by pressing a key like I dont know E for example. I couldnt find any solid tutorials to help me so I just been putting things together and hoping it worked, I managed to have a script up and running where apon startup the item would randomly spawn between specified locations but now nothing works

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript {
    5.  
    6.     // Use this for initialization
    7.     int myInt;
    8.  
    9.     void Start () {
    10.         myInt = Random.Range (1, 101);
    11.         if (myInt <= 50) {
    12.             gameObject.transform.position = new Vector3 (29, 2, -78);
    13.                 } else {
    14.             gameObject.transform.position = new Vector3 (31, -4, -75);
    15.         }
    16.     }
    17.  
    18.     void onTriggerEnter(Collider collider){
    19.                 if (collider.gameObject.name == "OVRPlayerController" && Input.GetKeyDown ("E")) {
    20.                         ItemCount++;
    21.             gameObject.transform.position = new Vector3 (41, -1, -10);
    22.                 }
    23.         }
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.    
    29.     }
    30. }
    31.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    1) OnTriggerEnter starts with a capital letter, using onTriggerEnter will make the program interpret it as a different method
    2) you code will most likely still not work, as OnTriggerEnter is called only once, you must be lucky to hit the e-key at the "same time"
    3) spawn/move the item to random locations, at the moment you spawn them at hardcoded position using literals.
     
  3. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    But I've definitely seen people with unity games where they can hit a key to pick up something that's in reach, anyway I somehow managed to get rid of all the bugs, but it still doesn't do the pickup. Could someone please point me in the right direction.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     int myInt;
    8.  
    9.     void Start () {
    10.         myInt = Random.Range (1, 101);
    11.         if (myInt <= 50) {
    12.             gameObject.transform.position = new Vector3 (29, 2, -78);
    13.                 } else {
    14.             gameObject.transform.position = new Vector3 (31, -4, -75);
    15.         }
    16.     }
    17.  
    18.     void OnTriggerEnter(Collider collider){
    19.                 if (collider.gameObject.name == "OVRPlayerController" && Input.GetKeyDown ("K")) {
    20.                         ItemCount.ItemCounter++;
    21.             gameObject.transform.position = new Vector3 (41, -1, -10);
    22.                 }
    23.         }
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.    
    29.     }
    30. }
    31.  
     
  4. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    Physics.Raycast() will create a line which length can be specified. It returns true if it detects some object (you can even specify which one).
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Oh sorry, i totally forget to add that. xD
    There are different approaches to pick it up.

    E.g. there''s also OnTriggerStay, which will be called as long as you stay in the trigger. You can use that instead of OnTriggerEnter. Then it should actually work.

    Another different way: as soon as you enter a trigger (using OnTriggerEnter) you could add the item into a list, let's call it something like 'pickableItems'.
    As soon as you leave the trigger (OnTriggerExit), remove it from the list.
    You can then always pick up all items in the list when the key is pressed, but you should also remove them in order to avoid bugs.

    Both methods should work well, the second one des not rely on continuous OnTriggerStay calls though, that just came to my mind thinking about an alternative as i don't know what's the additional overhead when many OnTriggerStays are called.
     
    Stoven likes this.