// Update is called once per frame
    void Update()
    {
        // first: find out where the player is clicking
        // take player's mouse cursor position, and "project" out from camera's facing
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        // if we want "forensics" information about where the raycast hit, we need a blank variable
        RaycastHit rayHit = new RaycastHit();

        if (Physics.Raycast(ray, out rayHit, 1000f))
        {
            // now, if player clicks and NOT on an NPC, then instantiate a new NPC
            if (Input.GetMouseButtonDown(0) && rayHit.collider.tag != "Player")
            {
                Vector3 spawnPosition = rayHit.point + rayHit.normal;
                // if you want to REMEMBER the clone you instantiate, make sure you cast the type ("as Raycast") at the end
                NPCRaycast newNPC = Instantiate(npcPrefab, spawnPosition, Quaternion.Euler(0, 0, 0)) as NPCRaycast;
                allMyNpcs.Add(newNPC);                    // add the clone to the list of NPCs (see above)
            }
        }

        // if we press R, then go through the entire list and multiply each NPC's speed by 2
        if (Input.GetKeyDown(KeyCode.R))
        {
            // "var" infers the type based on available information. In this case, we already know allMyNpcs is a list of NPCRaycast objects
            foreach (var thisNPC in allMyNpcs)       // "foreach" is a shortcut "for" loop designed for iterating through arrays and lists
            {
                thisNPC.speed *= 2f;                 // we can access "speed" from this script because it is a public variable in NPCRaycast.cs
            }
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        Ray        ray    = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit rayHit = new RaycastHit();

        if (Physics.Raycast(ray, out rayHit, 100f))
        {
            if (Input.GetMouseButton(0) && rayHit.collider.tag != "Player")
            {
                NPCRaycast newNPC = Instantiate(npcPrefab, rayHit.point, Quaternion.Euler(0f, 0f, 0f)) as NPCRaycast;
                allMyNpcs.Add(newNPC);
            }
        }
    }