Hi all, this is my first time on these forums so forgive me if im being stupid or anything.
Last night I started to try andconvert a youtube tut trying to explain and show one how to code a minecraft scenario wherby block can be instantiated by a left mouseclick and a range of block types could be coded and changed via right mousecllick.
Me being me, had to NOT just follow his javascript example but try and change it to a C# implementation.
This is where im having a bit of bother. Below is the code , and i know its something really silly like i am missing a simple typecast or i havent set up my raycast correctly or i havent filled enough arguements into my new Transform instantiaition.
ANyway, hoope this is soo obvious a mistake to make for a noob, I dont get flamed too much form not knowing already.
Thanks for reading
Gruffy
using UnityEngine;
using System.Collections;
public class BlockCreator : MonoBehaviour
{
Transform blockOne;
Transform blockTwo;
float blockSelected;
float range = 100f;
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Update is called once per frame
void FixedUpdate ()
{
if(Input.GetKeyDown (KeyCode.Q))
{
blockSelected -= 1;
}
if(Input.GetKeyDown (KeyCode.E))
{
blockSelected += 1;
}
if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown (1))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
//Rigidbody instance = Instantiate(hit, transform.position, transform.rotation) as Rigidbody;
//vector to represent forward direction of the current transform
//Vector3 fwd = transform.TransformDirection(Vector3.forward);
//instance.Add(fwd);//
if(Input.GetMouseButtonDown (1))
{
if(blockSelected == 1)
{ //Rigidbody instance = Instantiate(blockOne, transform.position, transform.rotation) as Rigidbody
//grassBlock = new Transform();
blockOne = new Transform(Instantiate(blockOne, hit.collider.transform.position + hit.normal.normalized, Quaternion.identity));
//grassBlock = Instantiate(blockTwo, hit.collider.transform.position + hit.normal.normalized,0,0, Quaternion.identity);
blockOne.tag = "blockOne";
}
if(blockSelected == 2)
{
blockTwo = new Transform(Instantiate(blockTwo, hit.collider.transform.position + hit.normal.normalized, Quaternion.identity));
blockTwo.tag = "blockTwo";
}
}
}
}
// {
//
// //Transform viewDir = new Vector3(transform.TransformDirection(Vector3.left));
//
// if(Physics.Raycast(ray, hit, range))
////
//// Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//// Vector3 fwd = transform.TransformDirection(Vector3.forward);
//// if (Physics.Raycast (ray, out hit, 100))
// {
//
// //Debug.DrawLine (ray.origin, hit.point);
// //instantiate projectile - (overloads) what ?, where ?, a rotation ?
// //Rigidbody instance = Instantiate(blockOne(hit.collider.transform.position), transform.position, transform.rotation) as Transform;
// //vector to represent forward direction of the current transform
// //Vector3 fwd = transform.TransformDirection(Vector3.forward);
//
else
{
Destroy(hit.transform.gameObject);
}
}
}