void Mine() { if (anim) { anim.SetTrigger("Mine"); } // anim.Play("Mining"); RaycastHit hit; //if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) if (Physics.Raycast(myCam.transform.position, myCam.transform.forward, out hit, 5)) { Vector3 pos = new Vector3(EditTerrain.GetBlockPos(hit).x, EditTerrain.GetBlockPos(hit).y, EditTerrain.GetBlockPos(hit).z); Block block = EditTerrain.GetBlock(hit); EditTerrain.SetBlock(hit, new BlockAir(), false, true); if (block is BlockGrass) { Instantiate(item_grass, pos, Quaternion.identity); } else if (block is BlockSand) { Instantiate(item_sand, pos, Quaternion.identity); } else if (block is BlockWood) { Instantiate(item_wood, pos, Quaternion.identity); } } }
protected override void HandleInput() { base.HandleInput(); if (!pauseMenuUp) { if (Input.GetMouseButtonDown(1)) { RaycastHit hit; if (Physics.Raycast(transform.GetChild(0).position, transform.GetChild(0).forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } else if (Input.GetMouseButtonDown(0)) { RaycastHit hit; if (Physics.Raycast(transform.GetChild(0).position, transform.GetChild(0).forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockDirt(), true); } } } transform.position += transform.forward * 3 * Input.GetAxis("Vertical"); transform.position += transform.right * 3 * Input.GetAxis("Horizontal"); transform.position += transform.up * 3 * Input.GetAxis("UpDown"); }
public void AttemptMove(int xDir, int yDir) { Vector2 end = rb2D.position + new Vector2(xDir, yDir); RaycastHit2D fogDetect; RaycastHit2D walldetect; fogDetect = Physics2D.Raycast(rb2D.position, end); Debug.Log(end.y); walldetect = Physics2D.Linecast(rb2D.position, end, wall); WorldPos pos = EditTerrain.GetBlockPos(end); Debug.Log(pos.y); if (fogDetect) { fogWorld.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty)); } //if (fogDetect) //{ // GameObject.Find(fogDetect.transform.name).GetComponent<SpriteRenderer>().enabled = false; // print(fogDetect.transform.name); // GameObject.Find(fogDetect.transform.name).GetComponent<BoxCollider2D>().enabled = false; // Destroy(GameObject.Find(fogDetect.transform.name).GetComponent<GameObject>()); //} if (!walldetect) { //print(walldetect.transform.name); rb2D.MovePosition(end); //print("Moved"); } StartCoroutine(Delay()); }
void Update() { if (Input.GetKeyDown(KeyCode.LeftControl)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { if (World.singleton.isClient) { NetworkBlocksClient.SetBlock(hit, new BlockAir()); } else { EditTerrain.SetBlock(hit, new BlockAir()); } } } rot = new Vector2( rot.x + Input.GetAxis("Mouse X") * 2, rot.y + Input.GetAxis("Mouse Y") * 2); transform.localRotation = Quaternion.AngleAxis(rot.x, Vector3.up); transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left); transform.position += transform.forward * 2 * Input.GetAxis("Vertical"); transform.position += transform.right * 2 * Input.GetAxis("Horizontal"); }
public void AttemptMove(int xDir, int yDir) { if (Time.timeScale == 1) { Vector2 end = rb2D.position + new Vector2(xDir, yDir); RaycastHit2D fogDetect; RaycastHit2D walldetect; fogDetect = Physics2D.Linecast(rb2D.position, end, fog); walldetect = Physics2D.Linecast(rb2D.position, end, wall); //nekeisti WorldPos pos = EditTerrain.GetBlockPos(fogDetect); //Debug.Log(pos.x + " " + pos.y); if (fogDetect) { fogWorld.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty)); scoreManager.AddPoints(1); } //if (fogDetect) //{ // GameObject.Find(fogDetect.transform.name).GetComponent<SpriteRenderer>().enabled = false; // print(fogDetect.transform.name); // GameObject.Find(fogDetect.transform.name).GetComponent<BoxCollider2D>().enabled = false; // Destroy(GameObject.Find(fogDetect.transform.name).GetComponent<GameObject>()); //} if (!walldetect && !fogDetect) { gameSounds.PlaySound(rnd.Next(0, 2)); rb2D.MovePosition(end); } } StartCoroutine(Delay()); }
/// <summary> /// Handles the mouse input for the player /// </summary> void HandleMouseInput() { // if the cursor is not locked, just return if (Cursor.lockState == CursorLockMode.None) { return; } float xMovement = Input.GetAxisRaw("Mouse X") * sensitivity; float yMovement = Input.GetAxisRaw("Mouse Y") * sensitivity; // apply the x-movement (rotation around y-axis) to the rigidbody transform.Rotate(Vector3.up * xMovement); // apply the y-movement (rotation around the x-axis) to the camera) transform.GetChild(0).transform.Rotate(Vector3.right * -yMovement); // handle any click event if (Input.GetMouseButtonDown(0)) { RaycastHit raycastHit; Transform camera = transform.GetChild(0).transform; if (Physics.Raycast(camera.position, camera.forward, out raycastHit, 100)) { Vector3 blockPos = EditTerrain.GetBlockPos(raycastHit); EditTerrain.SetBlock(raycastHit, new AirBlock(blockPos)); } } }
void Update() { if (lazer) { if (Input.GetKey("q")) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } } else { if (Input.GetKeyDown(KeyCode.Q)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } } rot = new Vector2( rot.x + Input.GetAxis("Mouse X") * 3, rot.y + Input.GetAxis("Mouse Y") * 3); transform.localRotation = Quaternion.AngleAxis(rot.x, Vector3.up); transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left); /*transform.position += transform.forward * 3 * Input.GetAxis("Vertical"); * transform.position += transform.right * 3 * Input.GetAxis("Horizontal");*/ }
// Update is called once per frame void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); horizontal = horizontal * 5 * Time.deltaTime; vertical = vertical * 5 * Time.deltaTime; Vector3 position = new Vector3(transform.position.x + horizontal, transform.position.y + vertical, transform.position.z); transform.position = position; if (Input.GetKeyDown(KeyCode.Space)) { Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero); WorldPos pos = EditTerrain.GetBlockPos(hit); if (hit.collider != null && hit.collider.GetComponent <Grid>()) { fog.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty)); } } if (Input.GetMouseButtonDown(1)) { cam.orthographicSize = 5; } if (Input.GetMouseButtonUp(1)) { cam.orthographicSize = 8; } }
private void OnCollisionEnter2D(Collision2D collision) { WorldPos pos = EditTerrain.GetBlockPos(collision.contacts[0].point); if (collision.collider.GetComponent <Grid>()) { fog.SetTile(pos.x, pos.y, new GridTile(GridTile.TileTypes.Empty)); } }
private void Update() { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.down, out hit, 1f)) { standingOn = EditTerrain.GetBlock(hit); } position = new Vector3(Mathf.Round(transform.position.x * 100) / 100, Mathf.Round(transform.position.y * 100) / 100, Mathf.Round(transform.position.z * 100) / 100); }
void Update() { ray = cam.ScreenPointToRay(Input.mousePosition); if (Input.GetMouseButton(0)) { RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } }
void Update() { if (!isLaser) //1 click { if (Input.GetKeyDown(KeyCode.Mouse0)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } } else //laser { if (Input.GetKey(KeyCode.Mouse0)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } } if (Input.GetKeyDown(KeyCode.Mouse1)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, DefineSelectedBlock(selectedBlockName), true); } } if (Input.GetKeyDown(KeyCode.R)) { SceneManager.LoadScene(0); } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } /*rot = new Vector2( * rot.x + Input.GetAxis("Mouse X") * 3, * rot.y + Input.GetAxis("Mouse Y") * 3); * * transform.localRotation = Quaternion.AngleAxis(rot.x, Vector3.up); * transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left); * * transform.position += transform.forward * 3 * Input.GetAxis("Vertical"); * transform.position += transform.right * 3 * Input.GetAxis("Horizontal");*/ }
public void AddToGarden(string name, int numStock) { Debug.Log(name); Debug.Log(numStock); Debug.Log(companyGardenPositions); if (companyGardenPositions.Contains(name)) { Vector3 pos = (Vector3)companyGardenPositions[name]; Block block = CreateBlock(name); block.selling = true; SetBlock(EditTerrain.GetBlockPos(pos).x, EditTerrain.GetBlockPos(pos).y + numStock, EditTerrain.GetBlockPos(pos).z, block); } }
//Function which allows player to collect ammo void CollectBricks() { //If ray from camera hits block then collect it ray = cam.ScreenPointToRay(Input.mousePosition); if (Input.GetMouseButtonDown(1)) { RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); Inventory.ammo += 1; } } }
public static void SetBlock(RaycastHit hit, Block block, bool adjacent = false) { if (!singleton.isConnected) { Debug.LogError("Cannont set block client is not connected"); return; } WorldPos pos = EditTerrain.GetBlockPos(hit, adjacent); var msg = new MessaageTypes.SetBlockMessage(); msg.pos = pos; msg.blockID = BlockIDManager.GetID(block); singleton.client.Send(MessaageTypes.SetBlockID, msg); }
void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, interactionDistance)) { EditTerrain.SetBlock(hit, new Block_Air()); } } if (Input.GetMouseButtonDown(1)) { RaycastHit hit; if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, interactionDistance)) { EditTerrain.SetBlock(hit, new Block_Stone(), true); } } }
void FixedUpdate() { if (Vector3.Magnitude(rb.velocity) > 0.001f) { timer = 0; } //Debug.Log(rb.velocity); // Debug.Log(timer); if (thrown) { rb.AddForce(Physics.gravity * rb.mass * gravityMultiplier); timer += Time.deltaTime; if (timer > 5) { gameObject.SetActive(false); GameObject.Find("World").GetComponent <World>().SetBlock(EditTerrain.GetBlockPos(transform.position).x, EditTerrain.GetBlockPos(transform.position).y, EditTerrain.GetBlockPos(transform.position).z, CreateBlock()); } } }
protected override void HandleInput() { base.HandleInput(); if (!pauseMenuUp) { if (Input.GetMouseButtonDown(1)) { RaycastHit hit; if (Physics.Raycast(transform.GetChild(0).position, transform.GetChild(0).forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } else if (Input.GetMouseButtonDown(0)) { RaycastHit hit; if (Physics.Raycast(transform.GetChild(0).position, transform.GetChild(0).forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockDirt(), true); } } } }
void OnReceiveChunkData(NetworkMessage netMsg) { //Debug.Log("Recevied Chunk Data"); var msg = netMsg.ReadMessage <MessaageTypes.ChunkDataMessage>(); Chunk chunk = EditTerrain.GetChunk(msg.chunkPos); if (chunk == null) { Debug.Log("Chunk not loaded"); return; } foreach (MessaageTypes.MsgBlock msgBlock in msg.blocks) { Debug.Log(msg.chunkPos.x + "," + msg.chunkPos.y + "," + msg.chunkPos.z); Debug.Log(msgBlock.x + "," + msgBlock.y + "," + msgBlock.y); World.singleton.SetBlock(msgBlock.x + msg.chunkPos.x, msgBlock.y + msg.chunkPos.y, msgBlock.z + msg.chunkPos.z, BlockIDManager.GetBlock(msgBlock.blockID)); } //If chunk is already rendered update and rerender it. if (chunk.rendered) { chunk.update = true; } }
// Update is called once per frame void FixedUpdate() { timer += Time.deltaTime; // if (Input.GetMouseButtonDown(0)) // if(leapFinger != null && leapFinger.Pinch && throwBlock == null) //if(pinch && throwBlock == null) if (pinch && timer > pinchTimer) { // RaycastHit hit = leapFinger.GetRaycast(); // RaycastHit hit; //Vector3 fwd = transform.TransformDirection(Vector3.forward); // if (Physics.Raycast(transform.position, fwd, out hit, destroyDist) && timer > 0.1) // { if (hit.collider.name.Contains("Chunk")) { timer = 0; Block block = EditTerrain.GetBlock(hit); string blockName = block.ToString(); if (blockName != "Air Block") { //Debug.Log(block.ToString()); string firstWord = blockName.IndexOf(" ") > -1 ? blockName.Substring(0, blockName.IndexOf(" ")) : blockName; if (firstWord == "Stock") { Debug.Log(blockName); blockName = blockName.Substring(blockName.IndexOf(" ") + 1); string companyName = blockName.Substring(0, blockName.IndexOf(" ")); Debug.Log(companyName); if (!block.selling) { if (!inventory.AddToInventory(companyName)) { return; } } else { inventory.RemoveFromInventory(companyName); } } else { return; } //create block to hold // throwBlock = (GameObject)Instantiate(Resources.Load(blockName), transform.position + transform.forward * 2 + transform.up * 0.5f, Quaternion.identity); // particles.SetActive(true); //float gravityMultiplier = GameObject.Find("Player").GetComponent<FirstPersonController>().m_GravityMultiplier; // throwBlock.GetComponent<PrefabBlockPhysics>().gravityMultiplier = gravityMultiplier; // if (gravityMultiplier < 0) // { // throwBlock.GetComponent<PrefabBlockTexture>().upsideDown = true; // } //delete block EditTerrain.SetBlock(hit, new BlockAir()); } } // else if (hit.collider.name.Contains("Block")) // { // throwBlock = (GameObject)Instantiate(hit.collider.gameObject, transform.position + transform.forward * 2, Quaternion.identity); // throwBlock.GetComponent<PrefabBlockPhysics>().Reset(); // if (hit.collider.gameObject.GetComponent<PrefabBlockPhysics>().gravityMultiplier < 0) // { // throwBlock.GetComponent<PrefabBlockTexture>().upsideDown = true; // } // Destroy(hit.collider.gameObject); // } //// } } // if (throwBlock != null && !throwBlock.GetComponent<PrefabBlockPhysics>().thrown) // { // throwBlock.transform.position = Vector3.MoveTowards(throwBlock.transform.position, transform.position + transform.forward * 2f + transform.up * 0.5f, 10 * Time.deltaTime); // particles.transform.position = throwBlock.transform.position; // } //if (Input.GetMouseButtonDown(1)) // if(pinch == false) // { // if (throwBlock != null && !throwBlock.GetComponent<PrefabBlockPhysics>().thrown) // { // throwBlock.GetComponent<PrefabBlockPhysics>().thrown = true; // Rigidbody rb = throwBlock.GetComponent<Rigidbody>(); // if (rb != null) // { // rb.useGravity = true; // rb.isKinematic = false; // rb.AddForce(transform.forward * 1000 + transform.up * 500); // particles.SetActive(false); // } // throwBlock = null; // } // } //moving controls /* * * rot = new Vector2(rot.x + Input.GetAxis("Mouse X") * speed, rot.y + Input.GetAxis("Mouse Y") * speed); * * transform.localRotation = Quaternion.AngleAxis(rot.x, Vector3.up); * transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left); * * transform.position += transform.forward * speed * Input.GetAxis("Vertical"); * transform.position += transform.right * speed * Input.GetAxis("Horizontal"); * */ }
// Update is called once per frame void Update() { if (Time.timeScale == 1 && !CharacterEvent.armed && CharacterEvent.unsheatle) { if (Input.GetMouseButtonDown(0)) { Ray ray = GetComponent <Camera> ().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } if (Input.GetMouseButtonDown(1)) { Ray ray = GetComponent <Camera> ().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { EditTerrain.SetBlock(hit, block, true); } } } /*if (!recuperateTown && Input.GetButtonDown ("R")) { * recuperateTown = true; * RecuperateTown rec = new RecuperateTown (); * rec.LoadTown (); * recuperateTown = false; * }*/ if (!CharacterEvent.armed) { scrollPosition = (scrollPosition - Mathf.CeilToInt(Input.mouseScrollDelta [1])) % 10; } if (scrollPosition < 0) { scrollPosition = 9; } switch (scrollPosition) { case 0: block = new Block(); cube.sharedMaterial = stone; break; case 2: block = new BlockDirt(); cube.sharedMaterial = dirt; break; case 1: block = new BlockGrass(); cube.sharedMaterial = grass; break; case 3: block = new BlockWood(); cube.sharedMaterial = wood; break; case 4: block = new BlockLeaves(); cube.sharedMaterial = leaves; break; case 5: block = new BlockWater(); cube.sharedMaterial = water; break; case 6: block = new BlockWoodPlanks(); cube.sharedMaterial = woodPlanks; break; case 7: block = new BlockStoneBricks(); cube.sharedMaterial = stoneBricks; break; case 8: block = new BlockGlass(); cube.sharedMaterial = glass; break; case 9: block = new BlockTile(); cube.sharedMaterial = tiles; break; } }
void Update() { rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime; rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime; rotationY = Mathf.Clamp(rotationY, -90, 90); transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up); transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left); if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime; transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime; } else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) { transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime; transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime; } else { transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime; transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime; } if (Input.GetKey(KeyCode.Q)) { transform.position += transform.up * climbSpeed * Time.deltaTime; } if (Input.GetKey(KeyCode.E)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; } if (Input.GetMouseButtonDown(0)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockAir()); } } if (Input.GetMouseButtonDown(1)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) { EditTerrain.SetBlock(hit, new BlockGrass(), true); } } if (Input.GetMouseButtonDown(2)) { RaycastHit hitInfo; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo)) { int x = Mathf.CeilToInt(hitInfo.point.x); int y = Mathf.CeilToInt(hitInfo.point.y); int z = Mathf.CeilToInt(hitInfo.point.z); Block bl = world.GetBlock(x, y, z); Block downBl = world.GetBlock(x, y - 1, z); //Check to see if we got a water block ///--- if (bl.GetType() == typeof(BlockAir) && downBl.GetType() != typeof(BlockWater)) { //set the new water block world.SetBlock(x, y, z, new BlockWater()); //get the block we just placed BlockWater newBlock = world.GetBlock(x, y, z) as BlockWater; newBlock.SetWorld(world, x, y, z); //and add it to register for update newBlock.RegisterForUpdate(); } } } if (Input.GetKeyDown(KeyCode.LeftAlt)) { Cursor.lockState = (Cursor.lockState == CursorLockMode.Locked) ? CursorLockMode.Locked : CursorLockMode.None; } }
//private List<Vector3> WaitingToAddBlocks = new List<Vector3>(); // Update is called once per frame protected override void Update() { base.Update(); //Releasing left click //Places block at position if (Input.GetMouseButtonUp(0)) { Add.transform.GetComponent <MeshRenderer>().enabled = false; Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 30, ~(1 << 8))) { EditTerrain.SetBlock(hit, new BlockDirt(), true); } } //Holding left click //Shows Add block on screen else if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 30, ~(1 << 8))) { Add.transform.GetComponent <MeshRenderer>().enabled = true; Vector3 rawposition = hit.point + hit.normal / 2; Vector3 roundedpostiion = new Vector3(Mathf.RoundToInt(rawposition.x), Mathf.RoundToInt(rawposition.y), Mathf.RoundToInt(rawposition.z)); Add.transform.position = roundedpostiion; } else { Add.transform.GetComponent <MeshRenderer>().enabled = false; } } //not holding left click //Releasing right click //Deletes block at position if (Input.GetMouseButtonUp(1)) { Delete.transform.GetComponent <MeshRenderer>().enabled = false; Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 30, ~(1 << 8))) { EditTerrain.SetBlock(hit, new BlockAir()); } } //Holding right click //Shows delete on screen else if (Input.GetMouseButton(1)) { Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit, 30, ~(1 << 8))) { Delete.transform.GetComponent <MeshRenderer>().enabled = true; Vector3 rawposition = hit.point - hit.normal / 2; Vector3 roundedpostiion = new Vector3(Mathf.RoundToInt(rawposition.x), Mathf.RoundToInt(rawposition.y), Mathf.RoundToInt(rawposition.z)); Delete.transform.position = roundedpostiion; } else { Delete.transform.GetComponent <MeshRenderer>().enabled = false; } } }
void Update() { if (Input.GetKeyDown("1")) { currentBlock = new BlockGrass(); GetComponent <InventoryManager>().SelectItem(1); } else if (Input.GetKeyDown("2")) { currentBlock = new BlockSand(); GetComponent <InventoryManager>().SelectItem(2); } else if (Input.GetKeyDown("3")) { currentBlock = new BlockWood(); GetComponent <InventoryManager>().SelectItem(3); } if (Input.GetKeyDown(KeyCode.Q)) { GameObject temp; if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.grass) { temp = Instantiate(item_grass, transform.position + transform.forward, transform.rotation); temp.GetComponent <Rigidbody>().AddForce(transform.forward * 3 + new Vector3(0, 1, 0), ForceMode.Impulse); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } else if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.sand) { temp = Instantiate(item_sand, transform.position + transform.forward, transform.rotation); temp.GetComponent <Rigidbody>().AddForce(transform.forward * 3 + new Vector3(0, 1, 0), ForceMode.Impulse); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } else if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.wood) { temp = Instantiate(item_wood, transform.position + transform.forward, transform.rotation); temp.GetComponent <Rigidbody>().AddForce(transform.forward * 3 + new Vector3(0, 1, 0), ForceMode.Impulse); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } } if (Input.GetMouseButton(0)) { if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.bow) { ChargeArrow(); } } if (Input.GetMouseButtonDown(0)) { if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.pick) { Mine(); } } if (Input.GetMouseButtonUp(0)) { if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.bow) { FireArrow(); } } if (Input.GetMouseButtonDown(1)) { RaycastHit hit; //if (Physics.Raycast(transform.position, transform.forward, out hit, 100)) if (Physics.Raycast(myCam.transform.position, myCam.transform.forward, out hit, 5)) { //EditTerrain.SetBlock(hit, new BlockSand()); // Get block to the side which faces the player //EditTerrain.SetSideBlock(hit, new BlockSand(), false, true); if (GetComponent <InventoryManager>().currentTool) { if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.grass) { EditTerrain.SetSideBlock(hit, new BlockGrass(), false, true); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } else if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.sand) { EditTerrain.SetSideBlock(hit, new BlockSand(), false, true); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } else if (GetComponent <InventoryManager>().currentTool.GetComponent <Item>().type == Item.Items.wood) { EditTerrain.SetSideBlock(hit, new BlockWood(), false, true); GetComponent <InventoryManager>().inv[GetComponent <InventoryManager>().selectedIndex].count--; } } } } }
//TODO clean this up to reduce raycasts void Update() { //if (GetComponent<FirstPersonControllerCustom>().inputLocked) //return; //Block selector RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore)) { if (!blockSelector.gameObject.activeSelf) { blockSelector.gameObject.SetActive(true); } hit.point += (-hit.normal * 0.1f); //Smudging in a bit to fix edge case Vector3Int pos = EditTerrain.GetBlockPos(hit); blockSelector.position = pos.ToVector3(); } else { if (blockSelector.gameObject.activeSelf) { blockSelector.gameObject.SetActive(false); } } if (currentBlockPlaceCooldown > 0f) { currentBlockPlaceCooldown -= Time.deltaTime; return; } //Break block if (PlayerInputManager.input.Attack.IsPressed) { if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) { { EditTerrain.BreakBlock(hit); currentBlockPlaceCooldown = blockPlaceCooldown; } } //Place block if (PlayerInputManager.input.Use.IsPressed) { // RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f)), out hit, maxReach, layerMask, QueryTriggerInteraction.Ignore))//, int.MaxValue, QueryTriggerInteraction.Ignore)) { { if (GetComponent <PlayerInventory>().CurrentActiveItem != null && GetComponent <PlayerInventory>().CurrentActiveItem.placeable) { hit.point += hit.normal; Vector3Int pos = EditTerrain.GetBlockPos(hit); Collider[] cols = Physics.OverlapBox(pos.ToVector3(), Vector3.one * 0.45f, Quaternion.identity, LayerMask.GetMask("Default", "Blocks"), QueryTriggerInteraction.Ignore); if (cols.Length > 0) { //Something in way foreach (Collider col in cols) { if (col.GetComponentInChildren <Renderer>() != null) { StartCoroutine(TempFlashRed(col.GetComponentInChildren <Renderer>())); } } } else { // RaycastHit hit2; // if (Physics.BoxCast(pos.ToVector3(), Vector3.one * 0.35f, Vector3.up * 0.001f, out hit2)) // { // Debug.LogError(pos.ToVector3() + " - " + hit2.collider.gameObject.name + " - " + hit2.distance); // ExtDebug.DrawBoxCastOnHit(pos.ToVector3(), Vector3.one * 0.35f, Quaternion.identity, Vector3.up * 0.001f, 0f, Color.red); // } //TODO fix EditTerrain.PlaceBlock(hit, FindObjectOfType <PlayerInventory>().CurrentActiveItem.placeableBlockID); GetComponent <PlayerInventory>().ConsumeCurrentItem(); currentBlockPlaceCooldown = blockPlaceCooldown; } } } } }