Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Inventory { public List items; public Inventory() { items = new List (); } Create an instance

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class Inventory {

public List items;

public Inventory() {

items = new List();

}

Create an instance (a variable) of Inventory in Robot Kyle's Movement Script. Don't forget to bring it to life in the constructor.

The next thing you'll want to do create a method to add items to the List. The method should take in a Transform as a parameter (called t ). To add to a list, you just call its Add( ) method and pass it the thing you want to add, which in this case would be items.Add(t);

Create a public method called AddToInventory( ) that takes in Transform as a parameter and adds it to the list.

After adding t to the list, set it to be inactive by calling t.gameObject.SetActive(false); This way, the game object will no longer be seen.

Finally, go back to the Movement Script and look at OnTriggerEnter(). Now pay attention because this part can be confusing. If you look at the parameter to this method (called other), you'll notice that it is of type Collider. The collider is attached to the model that Robot Kyle collided with. HOWEVER, remember that the model is a child of an empty. SO, we need to get the transform of the collider (the thing the collider is attached to) and get its parent. Do the following:

Remove the Debug print statement in OnTriggerEnter( )

Call the inventory's AddToInventory( ) method, passing other.transform.parent.

image text in transcribed

MonoDevelop-Unity Q Search No selection 7 public class Movement Script MonoBehaviour Animator anin; public float runspeed = 2.0f; 10 11 public bool isActive=true; 12 private float he.of: // Horizontal input (A, D) 13 private float v 0.0f; // Vertical input (W, S) 14 private float vAbs, hAbs, maxInput; II The absolute value of the input 15 private float dfs0.f; II The direction the character is facing 16 private float radsToDegs; // A conversion between radians and degrees 17 18 /I Use this for initialization 9 void Start) 20 21 anin GetComponent 0.1) (hAbs>8.11) dfs = Mathf.Atan2 (-v, h); 45 46 47 48 tretsfo Ioluot^arelookAtvargeti:nAngleakis (efs - radsTodegs, Vector'3.up)- vector. forvard - transfom.posit lonj transforn.LookAt oktTarget); transforn. Translate (Vector3.forward Time.deltaTime maxInput runSpeed); AErrors MonoDevelop-Unity Q Search No selection 7 public class Movement Script MonoBehaviour Animator anin; public float runspeed = 2.0f; 10 11 public bool isActive=true; 12 private float he.of: // Horizontal input (A, D) 13 private float v 0.0f; // Vertical input (W, S) 14 private float vAbs, hAbs, maxInput; II The absolute value of the input 15 private float dfs0.f; II The direction the character is facing 16 private float radsToDegs; // A conversion between radians and degrees 17 18 /I Use this for initialization 9 void Start) 20 21 anin GetComponent 0.1) (hAbs>8.11) dfs = Mathf.Atan2 (-v, h); 45 46 47 48 tretsfo Ioluot^arelookAtvargeti:nAngleakis (efs - radsTodegs, Vector'3.up)- vector. forvard - transfom.posit lonj transforn.LookAt oktTarget); transforn. Translate (Vector3.forward Time.deltaTime maxInput runSpeed); AErrors

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions