Dynamic Placement of Object Instantiate using RaycastHit
Hi and thanks for stopping by. Here is my script (running on void Update()).
void Update (){
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit, fixDist)) { //Using a raycast in direction forward from player transform
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
Debug.Log ("Did Hit");
if (hit.collider != null && Input.GetMouseButtonDown (0) || hit.collider != null && Input.GetKeyDown (KeyCode.P)) {//if there is a hit registered
GameObject whatever = ((GameObject)Instantiate (web, hit.point, transform.rotation));// spawn a new web game Object
whatever.gameObject.SendMessage ("ConnectRigidbody", hit.collider.gameObject.GetComponent<Rigidbody> ()); //Tell this game Object to connect to the aimed at rigidbody
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.white);
spinningWeb = true;
if (hit.collider != null && !Input.GetMouseButtonUp (0) || hit.collider != null && !Input.GetKeyUp (KeyCode.P)) {//if the player is holding down the fire key and is in range
whatever.transform.position = hit.point;
if (hit.collider != null && Input.GetMouseButtonUp (0) || hit.collider != null && Input.GetKeyUp (KeyCode.P)) {//if the player releases the fire key while aiming at something
whatever.SendMessage ("AnchorWeb", hit.point);
}
}
}
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.black);
Debug.Log("Did not Hit");
}
}
I am trying to make it so that WHILE the player is holding down Mousebutton(0) or KeyCode.P that the instantiated object's transform would follow the hit.point of my raycast. Upon release the object.whatever should anchor itself to the hit.point. Problem is that the code seems ONLY to instantiate whatever.gameObject at hit.point and nothing further.
P.S. Sorry if my code sample is messy. Will try to tidy it up. Don't hesitate to ask me questions.
Answer by qwertyjulz · Jan 24, 2019 at 07:28 AM
After some consultation I have a solution;
What has been done is that we created a variable outside of the Update function to store the hit.point Vector3 returned via the RaycastHit. Then, because our variable is now global, instead of originally being created within the RaycastHit if statement, we can access it outside the function.
So then we create a coroutine to house the while loop which will position the instantiated gameobject at the hit.point of our raycast forward out from the player position.
public class RayCastTest : MonoBehaviour {
public float targetDistance;
public GameObject web;
int lengthOfWeb;
public bool spinningWeb;
public Camera cam;
public Vector3 hitpoint;
public GameObject lastLink;
public LayerMask allowed;
float fixDist;
// Update is called once per frame
void Start ()
{
fixDist = 5f;
}
void Update (){
// Bit shift the index of the layer (8) to get a bit mask
int layerMask = 1 << 8;
// This would cast rays only against colliders in layer 8.
// But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
layerMask = ~layerMask;
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit, fixDist, allowed)) { //Using a raycast in direction forward from player transform
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
//Debug.Log ("Did Hit");
hitpoint = hit.point;
if (hit.collider != null && Input.GetMouseButtonDown (0) || hit.collider != null && Input.GetKeyDown (KeyCode.P)) {//if there is a hit registered
GameObject whatever = ((GameObject)Instantiate (web, hitpoint, transform.rotation));// spawn a new web game Object
whatever.gameObject.SendMessage ("ConnectRigidbody", hit.collider.gameObject.GetComponent<Rigidbody> ()); //Tell this game Object to connect to the aimed at rigidbody
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.white);
spinningWeb = true;
StartCoroutine (PlaceObject (hit, whatever));
if (hit.collider != null && Input.GetMouseButtonUp (0) || hit.collider != null && Input.GetKeyUp (KeyCode.P)) {//if the player releases the fire key while aiming at something
whatever.SendMessage ("AnchorWeb", hitpoint);
}
}
}
else
{
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.black);
//Debug.Log("Did not Hit");
}
}
IEnumerator PlaceObject(RaycastHit hit, GameObject whatever)
{ //while the player has the button pressed and is aimed over an object with a collider...
while (hit.collider != null && Input.GetMouseButton(0) || hit.collider != null && Input.GetKey (KeyCode.P)) {
whatever.transform.position = hitpoint; //set the transform of GameObject to hitpoint
Debug.Log (hitpoint + " whatever is "+ whatever);
if (Input.GetMouseButtonUp(0) ||Input.GetKeyUp (KeyCode.P)){
whatever.transform.position = hit.point;
}
yield return null;
}
}
}
Hope this helps.
Answer by ray2yar · Jan 23, 2019 at 09:56 PM
How often does this function get called?
The instantiate function? I am designing it to be the core game mechanic. So... often?
I am running the RaycastHit hit; on Update.