First off thank you for taking the time to read this an attempt to help me out, it is appreciated. That being said I am new to unity and am taking a game design class at my college.
Background: The game itself is a simple design as it is essentially a 3D ISPY/ Scavenger Hunt game. The player is able to move around in a 3D environment and look for said objects. The code is written in C#.
Problem: I am having problems with the syntax of actually raycasting a hit based on my mouse position && grabbing an object, in my case seeing if it is tagged “Findable” and if it is, the object in question will destroy upon clicking, which I will later trigger an animation off of. But the animation can wait until I figure out this scripting syntax. I do have some things commented out I plan on using for other things If I get the time in and outside of class to work on so please ignore those.
Snip-It of my script:
public float speed;
// public Text countText;
// public Text winText;
public float jumpSpeed;
private GameObject targetO;
// private Rigidbody rb;
// private int count;
void Start ()
{
// rb = GetComponent<Rigidbody>();
// count = 0;
// SetCountText ();
// winText.text = "";
}
// bool doJump = false;
void Update ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical );
movement = Camera.main.transform.TransformDirection(movement);
GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);{
if (Input.GetKeyDown ("space")) {
Vector3 jump = new Vector3 (0.0f, jumpSpeed, 0.0f);
GetComponent<Rigidbody> ().AddForce (jump);
}
if (Input.GetKeyDown ("r")) {
Application.LoadLevel (0);
}
if (Input.GetButtonDown("Fire1")){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f)) {
Debug.DrawLine(ray.origin, hit.point);
}
Debug.Log (hit);
Debug.DrawRay(ray.origin, ray.direction);
targetO = hit.transform.gameObject;
if (targetO.CompareTag ("Findable"){
Destroy (targetO);
}
}
}
}
}
I am not sure how to complete what I want done. It makes logical sense in my head but I am unable to find the correct commands/syntax to accomplish what I want. In this case raycast to determine a hit, if it hits an object it should search to see if the object is tagged with the “Findable” tag. If said object is tagged Findable it will be destroyed.
Thank you in advance for anyone who decides to help or drop some knowledge on me.
- J