- Home /
Collision Crafting system
Im making a game that requires the player to interact with objects, and if lets say 1 wood collides with 1 other wood and the player presses a certain button, the wood turns into a wall, anyone know how i would accomplish this? For an example:
int wood = 2; // id of wood
if (wood collides){
if (input.getkeydown(keycode.f)){
wood = 4; // sets wood to another ID, i dont know if this is the best way to approach this, but it might work
}
}
Ok to be precise This is the script ive made so far, it doesnt "Work" but its getting somewhere.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CraftingSystem : MonoBehaviour { // Goes on every object that needs crafting.
public bool collided;
public string selfName;
public string collidedName;
public GameObject cube;
public bool InRange;
public bool LookAt;
// private float num = 1.0f;
// private Vector3 scale;
private void Update()
{
craftInstructions();
}
void Craft(int itemID, int outputID)
{
// Game IDs
int dirt = 1;
int stone = 2;
int wood = 3;
int log = 4;
int stick = 5;
int planks = 6;
if (itemID == 3 && outputID == 7)
{
Instantiate(cube, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z), Quaternion.identity);
Destroy(gameObject);
}
}
void craftInstructions() // Please, this will not work, It only crafts 2 things (Issue) regardless of what the materials are. Change this, also figure out how to connect to the prefabs folder without dragging stuff into it (Incase of unity update disconnecting all prefabs)
{
if (gameObject.transform.localScale.x < 0 && gameObject.transform.localScale.y < 0 && gameObject.transform.localScale.z < 0)
{
Destroy(gameObject);
}
if (collided)
{
if (Input.GetKeyDown(KeyCode.F) && InRange && LookAt)
{
Craft(3, 7);
}
}
}
void Items()
{
}
private void OnMouseOver()
{
LookAt = true;
}
private void OnMouseExit()
{
LookAt = false;
}
private void OnCollisionEnter(Collision collision)
{
collided = true;
}
private void OnCollisionStay(Collision collision)
{
collided = true;
}
private void OnCollisionExit(Collision collision)
{
collided = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "p_reach")
{
InRange = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "p_reach")
{
InRange = false;
}
}
}
Hi, How are you making colliding them together? Are you before instantiating them or something else?
so basically if the pllayerpresses F and its collided with lets say another peice of wood, if that peice of wood is; wood and if the player presses F itll form into 1 wall.
Answer by tormentoarmagedoom · Jun 07, 2018 at 07:37 AM
Good day.
You should learn to use colliders and its functions:
OnCollisionEnter()
OnCollisionExit()
....
And the same for Trigger (i supose you need trigger, not collisions)
OnTriggerEnter();
OnTriggerStay();
OnTriggerExit();
If you know nothing about this methods, you should go look some tutorials, manuals, API pages, etc...
Once you tried to do it, if dont get it, come here again and ask more specific question.
Bye!
Answer by MT369MT · Jun 07, 2018 at 04:38 PM
Use something like:
int id = 2;
Void OnCollisionStay (Collider Coll)
{
if (Input.GetKeyDown(KeyCode.f)
{
if (Coll.gameObject.GetComponent<ObjectScript>().id == 2)
{
id = 4;
Coll.gameObject.GetComponent<ObjectScript>().id = 4;
}
}
}
So you can handle the collisions and you can get and modify the variables of yours and collided object. I don’t know if you want that 1 wood + 1 wood = 2 stone or only 1. If it is only 1 you can for example destroy the object and instantiate a stone object in the same position.
Thank you for the support, now i dont know weather to make a crafting based system put ON the objects themselfs, or a Empty gameobject ($$anonymous$$anager) to test for collisions (Probably more easier but requires more effort)