I am trying to get the object which is hit by my raycast, but I'm having some trouble. All of the other examples I have seen people using seem to involve and additional "hit" in the Physics.Raycast method but I can't seem to add it, and when I create a RaycastHit = hit variable I can't seem to figure out how to define it as the object the Raycast collided with. This is my code with annotations showing what I am trying to do, nothing I have tried so far works, any help is greatly appreciated. Code (csharp): if (Input.GetKeyDown(KeyCode.D)) //Left { if (!Physics.Raycast(transform.position, new Vector3(-2f, 0f, 0f), 2f) && (canMove)) // Space is free { transform.forward = new Vector3(-1f, 0f, 0f); //Player transform change transform.Rotate(0, 90, 0); //Player rotates MoveLeft(); //Player moves left } else if (Physics.Raycast(transform.position, new Vector3(-2f, 0f, 0f), 2f) && (canMove)) // There is an object here { //RaycastHit hit; //hit = ??? //if (hit.collider.tag == "Block") //If the object is a block //{ //thisBlockMove = hit.GetComponent<BlockMove>(); //Get the Block Move component for this block //thisBlockMove.MoveLeft(); // Move this block left //} } }
You would use raycast to find a ray cast hit position, like mouse click. Clicking on a posion on a plane would cast a ray ad move the player to that position, like this code.. Code (CSharp): Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast (ray, out hitInfo)) { GetComponent<NavMeshAgent> ().SetDestination (hitInfo.point); }
Thanks, I've seen that kind of example, but my (Physics.Raycast) doesn't want an "Out" or "RaycastHit", it wants a Vector 3 and a distance, this is basically where I am confused.
On "D" the player is either moving left, not moving or doing anything if there is a wall there, or pushing a block left if there is a block there. What I'm doing is firing a raycast over a short distance, and a) deciding whether the player can move Left (if the space is free (!Physics.Raycast)), and b) if the space is not free, (if Physics.Raycast) checking what is there (a wall or a cube) and if it's a cube, I'm telling the cube to move left. In order to do that, I need to get the cubes movement component and run a method from it. Checking if the space is empty or if there is something there works fine, but I am struggling to get the info about what was hit and then using that info to access the hit object and run a method from one of it's components. I hope that makes sense, and thanks for your help.
What you are experiencing is known as "function overloading" or "method overloading" where you can write the same function but give it different parameters. Even a small glance at the manual will show you the number of different ways to use it them, but you can't make up your own way to use them. https://docs.unity3d.com/ScriptReference/Physics.Raycast.html shows some and autocomplete shows 16 variations.
Ok! I have to be honest, this concept is new to me, so thanks for pointing it out! Now I understand why others seemed to be using the same function with different parameters, making more sense now, thanks for your help.
Yes it's trivial, just write the same function twice to try it yourself: Code (CSharp): void PrintNumber(float a) { Debug.Log(a); } void PrintNumber(float a, float b) { Debug.Log(a + b); } So here we have the same function name that can be used in 2 different ways. Turns out the compiler and language design allows us to do this.