- Home /
Question by
Captain-Ravioli · Jul 08, 2020 at 05:03 PM ·
scripting problem
Pickup and mix ingredients
I know how to implement a pickup mechanic, but I don't know how to mix 2 or more ingredients into a recipe. Here's my code. 1. The food script
public FoodConfig food;
bool pickup = false;
bool finalProductExists = false;
int i;
int x;
Transform ingredient;
SpriteRenderer sprite;
Color32 originalColor;
Animator anim;
Player player;
// Start is called before the first frame update
void Start()
{
player = FindObjectOfType<Player>();
anim = GetComponent<Animator>();
sprite = GetComponent<SpriteRenderer>();
sprite.sprite = food.GetArtwork();
originalColor = sprite.color;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision == player.pickupCollider)
{
if (!pickup)
pickup = true;
sprite.color -= new Color32(25, 25, 25, 0);
}
else
{
pickup = false;
sprite.color = originalColor;
}
}
// Update is called once per frame
void Update()
{
if (transform.parent)
{
print(transform.parent.transform.childCount);
}
if (pickup && Input.GetKeyDown(KeyCode.E))
{
anim.SetBool("PlayerIsHolding", true);
transform.SetParent(player.transform);
sprite.color = originalColor;
CheckIfRecipeIsMade();
}
}
private void CheckIfRecipeIsMade()
{
if (transform.parent.transform.childCount > 1)
{
foreach (Transform ingredient in transform.parent.transform)
{
this.ingredient = ingredient;
if (ingredient.gameObject == gameObject) return;
for (int x = 0; x <= ingredient.GetComponent<Food>().food.GetIngredientOfWhat().Length; x++)
{
if (CheckForSameProduct(ingredient))
{
for (int i = 0; i <= food.GetIngredientOfWhat()[x].GetComponent<Food>().food.GetIngredients().Length;)
{
this.i = i;
foreach (GameObject ingrEdient in food.GetIngredientOfWhat()[x].GetComponent<Food>().food.GetIngredients())
{
if (GameObject.Find(ingrEdient.name))
{
i++;
}
}
if (i == food.GetIngredientOfWhat()[x].GetComponent<Food>().food.GetIngredients().Length)
{
finalProductExists = true;
}
}
}
}
}
if (finalProductExists && i == food.GetIngredientOfWhat()[x].GetComponent<Food>().food.GetIngredients().Length)
{
var finalProduct = Instantiate(food.GetIngredientOfWhat()[x], player.transform);
Destroy(ingredient.gameObject);
Destroy(gameObject);
}
else return;
}
}
private bool CheckForSameProduct(Transform ingredient)
{
for (int i = 0; i < ingredient.GetComponent<Food>().food.GetIngredientOfWhat().Length;)
{
for (int x = 0; x < ingredient.GetComponent<Food>().food.GetIngredientOfWhat().Length;)
{
if (ingredient.GetComponent<Food>().food.GetIngredientOfWhat()[i] == food.GetIngredientOfWhat()[x])
{
this.x = x;
return true;
}
else
{
i++;
}
}
}
for (int i = 0; i < ingredient.GetComponent<Food>().food.GetIngredientOfWhat().Length;)
{
for (int x = 0; x < ingredient.GetComponent<Food>().food.GetIngredientOfWhat().Length;)
{
if (ingredient.GetComponent<Food>().food.GetIngredientOfWhat()[i] == food.GetIngredientOfWhat()[x])
{
this.x = i;
return true;
}
else
{
x++;
}
}
}
return false;
}
2, The player Script, if it matters
[SerializeField] float moveSpeed = 7f;
Animator anim;
public Collider2D pickupCollider;
private void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Move();
}
private void SnapToGrid()
{
if (!Input.GetKeyDown(KeyCode.A) && !Input.GetKeyDown(KeyCode.D) && !Input.GetKeyDown(KeyCode.W) && !Input.GetKeyDown(KeyCode.S))
{
transform.position = new Vector3(Mathf.RoundToInt(transform.position.x),
Mathf.RoundToInt(transform.position.y));
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Walk Forward"))
{
anim.SetBool("WalkForward", false);
anim.SetBool("IdleForward", true);
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("Walk Back"))
{
anim.SetBool("WalkBack", false);
anim.SetBool("IdleBack", true);
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("Walk Right"))
{
anim.SetBool("WalkRight", false);
anim.SetBool("IdleRight", true);
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("Walk Left"))
{
anim.SetBool("WalkLeft", false);
anim.SetBool("IdleLeft", true);
}
}
}
private void Move()
{
if (Input.GetAxis("Horizontal") > 0)
{
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
anim.SetBool("WalkForward", false);
anim.SetBool("WalkBack", false);
anim.SetBool("WalkRight", true);
anim.SetBool("WalkLeft", false);
anim.SetBool("IdleForward", false);
anim.SetBool("IdleBack", false);
anim.SetBool("IdleRight", false);
anim.SetBool("IdleLeft", false);
}
else if (Input.GetAxis("Horizontal") < 0)
{
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
anim.SetBool("WalkForward", false);
anim.SetBool("WalkBack", false);
anim.SetBool("WalkRight", false);
anim.SetBool("WalkLeft", true);
anim.SetBool("IdleForward", false);
anim.SetBool("IdleBack", false);
anim.SetBool("IdleRight", false);
anim.SetBool("IdleLeft", false);
}
else if (Input.GetAxis("Vertical") > 0)
{
transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
anim.SetBool("WalkForward", false);
anim.SetBool("WalkBack", true);
anim.SetBool("WalkRight", false);
anim.SetBool("WalkLeft", false);
anim.SetBool("IdleForward", false);
anim.SetBool("IdleBack", false);
anim.SetBool("IdleRight", false);
anim.SetBool("IdleLeft", false);
}
else if (Input.GetAxis("Vertical") < 0)
{
transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
anim.SetBool("WalkForward", true);
anim.SetBool("WalkBack", false);
anim.SetBool("WalkRight", false);
anim.SetBool("WalkLeft", false);
anim.SetBool("IdleForward", false);
anim.SetBool("IdleBack", false);
anim.SetBool("IdleRight", false);
anim.SetBool("IdleLeft", false);
}
else
{
SnapToGrid();
}
}
I need to know how to fix this as soon as possible. The problem is with the food script. Any help is appreciated!
Comment
Your answer
Follow this Question
Related Questions
Change two shader color with script 0 Answers
How to make W button move towards mouse direction 1 Answer
OnMouseDown only works once 1 Answer
On/Off Trigger is making Light Flicker 1 Answer
Simplify this code 3 Answers