hi there I am looking to have my blocks move in a direction away from the player it was both working at one point until I’ve added the raycast to detect the block underneath the raised blocks so it more then likely an issue with the area after Create RayCast for Smash Detection section
public class PlayerController : MonoBehaviour
{
//-----------------------------------------
// Variables
//-----------------------------------------
//player Controller
public int PlayerControllerNumber ;
public GameObject Player1;
public int BlockRaiseInput;
public int BlockSmash;
public GameObject InteractWith;
public GameObject InteractWithSmash;
public Vector3 Vector3Interactwith;
public Vector3 CubeVector;
public Vector3 PlayerVector;
public Transform NewBlockLocation;
public float MaxDistanceFromBlock;
public int NumberOfRaiseBlockDetected;
public bool EnableBlockingFall;
public bool HeightCheckerEnabled;
public GameObject BlockObject;
public float BlockCheckerDistance = 15f;
public LayerMask Mask;
public Vector3 DistanceFromCube;
void Start ()
{
PlayerControllerNumber++;
//Verifies which character player has control over
if(PlayerControllerNumber!=1)
{
if(PlayerControllerNumber==2)
{
}
if(PlayerControllerNumber==3)
{
}
if(PlayerControllerNumber==4)
{
}
}
}
// Update is called once per frame
void Update ()
{
if(InteractWith != null)
{
}
//------------------Player Movement Control ------------------------------------------------
// Move Player Left
if(Input.GetKey("a"))
{
Player1.transform.Translate(+.5f,0,0);
}
// Move Right
if(Input.GetKey("d"))
{
Player1.transform.Translate(-.5f,0,0);
}
// Move Up
if(Input.GetKey("w"))
{
Player1.transform.Translate(0,+.5f,0);
}
// Move Down
if(Input.GetKey("s"))
{
Player1.transform.Translate(0,-.5f,0);
}
//--------------------------------------------------------------------------------------
//------------------PlayerBlock Control ------------------------------------------------
//--------------------------------------------------------------------------------------
//Raise Block
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,Mask))
{
if(hit.transform != null)
{
HeightCheckerEnabled=true;
InteractWith=(hit.transform.gameObject);
Instantiate(InteractWith);
InteractWith.transform.Translate(0,5f,0);
//------------------------------------------
//-------Verify block beneath blocks------
//------------------------------------------
RaycastHit hitBlockChecker;
Ray BlockChecker = new Ray(transform.position,Vector3.down);
if (Physics.Raycast (BlockChecker,out hit,BlockCheckerDistance));
{
HeightCheckerEnabled=true;
{
print("There is something Underneath");
if(gameObject.CompareTag("BlockStatic"))
{
print("Block Underneath");
}
}
}
}
}
//Smash Block
if(Input.GetMouseButtonDown(1))
{
//-----------------------------------------------------
//--------Create RayCast for Smash Detection-----------
//-----------------------------------------------------
RaycastHit hitRmb;
Ray raySmash=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(raySmash,out hit,Mask))
{
if(hit.transform != null)
{
//Compare block location to Player to determine direction to push block
InteractWithSmash=(hit.transform.gameObject);
DistanceFromCube= InteractWithSmash.transform.position- Player1.transform.position ;
DistanceFromCube.Normalize();
CubeVector = InteractWithSmash.transform.position;
if(InteractWithSmash != null)
{
//-----------------------------------------------------
//---------Player position relative to Cube------------
//-----------------------------------------------------
//Correct Positioning for blocks
if(DistanceFromCube.x <0.9234627 && DistanceFromCube.x > 0.6221956 && DistanceFromCube.z > -0.1377406 && DistanceFromCube.z <0.9704086)
//0.923507
{
print("player is on Left");
print("Smash Right");
InteractWith=(hit.transform.gameObject);
InteractWith.transform.Translate (5,0,0);
//-0.1377406
}
if(DistanceFromCube.x > -3.000002 && DistanceFromCube.x< -0.9004511 && DistanceFromCube.z > -0.3284404 && DistanceFromCube.z < 0.420628 )
{
print("player is on Right");
print("Smash Left");
InteractWith=(hit.transform.gameObject);
InteractWith.transform.Translate (-5,0,0);
}
//Working
if(DistanceFromCube.z < 0.9729806 && DistanceFromCube.z>0.9043628 && DistanceFromCube.x < 0.04436589 && DistanceFromCube.x >-0.3276541)
//0.06402374 0.9793874
{
print("player is South");
print("Smash Up");
InteractWith=(hit.transform.gameObject);
InteractWith.transform.Translate (0,0,5);
}
if(DistanceFromCube.z > -0.9287744 && DistanceFromCube.z<-0.846046 && DistanceFromCube.x <0.09034547 && DistanceFromCube.x >-0.3276541)
{
print("player is North");
print("Smash Down");
InteractWith=(hit.transform.gameObject);
InteractWith.transform.Translate (0,0,-5);
//-0.9094616 -0.9710103
}
}
}
}
}
//----------------------------------------------------------------------------------------
//-------------------Menu Controls--------------------------------------------------------
//----------------------------------------------------------------------------------------
}
}
}