How to move Raycast Hit Object
Hey all,
I'm trying to move and object to the centre of the screen using ScreenToWorldPoint(); when hit by a Raycast from the camera. My code so far is:
public int sightDistance;
public Text uiText;
public RaycastHit hit;
public GameObject player;
public Camera main;
public bool selected = false;
void message () {
uiText.text = "HIT!";
}
void Update () {
if (selected == true) {
Vector3 p = main.ScreenToWorldPoint (new Vector3 (100, 100, main.nearClipPlane));
hit.collider.transform.position = p;
}
}
void FixedUpdate() {
RaycastHit hit;
Ray forwardRay = new Ray (transform.position, Vector3.forward);
if (Physics.Raycast (forwardRay, out hit, sightDistance)) {
if (hit.collider.tag == "pickable") {
message ();
hit.rigidbody.useGravity = false;
selected = true;
}
}
}
}
However, it doesn't give me errors and my game runs, but it doesn't work. How would I do this? I'm new to C# and Unity, and I would really appreciate the boost!
-spenmich
Comment