- Home /
Need help with Raycast
Hello guys. I have this script which works fine with the first script but I want the second script to be working 1st script;
public var CapsuleObject : GameObject;
private var CalButton = false;
function Update () {
CapsuleObject = GameObject.Find("Capsule");
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, 50))
{
CalButton = true;
}
}
function OnGUI () {
if(CalButton == true)
{
if(GUI.Button(Rect(100,100,100,50),"VerticalDistance"))
{
guiText.text = "" + CapsuleObject.transform.position.y;
}
}
}
and second script is;
public var CapsuleObject : GameObject;
private var CalButton = false;
function Update () {
var hit : RaycastHit;
CapsuleObject = GameObject.Find("Capsule");
if (Physics.Raycast(transform.position,transform.forward,hit,150))
{
CalButton = true;
}
}
function OnGUI ()
{
if(CalButton == true)
{
if(GUI.Button(Rect(100,100,100,50),"VerticalDistance"))
{
guiText.text = "" + CapsuleObject.transform.position.y;
}
}
}
The first one works well but the second one doesn't and I want the second one to be working. please help
You need to describe what you are trying to do and how it is not working. The difference between the two is that the first one is raycasting from the mouse position, and the second is raycasting from the position of the game object this script is attached to in the forward direction of that object. You might want to add this at line 9 to visualize the raycast:
Debug.DrawRay(transform.position, transform.forward * 150.0);
There are a couple of other issues with your code. Not sure if this is the way you want it, but once 'CalButton' gets set to true, it is true always regardless of the movement of this object. And second you are calling 'GameObject.Find()' in Update. $$anonymous$$ove this line into Start().
Thnx Robert for commenting. What I want that When I click on the "CalButton" then it the object needs to shoot another object, but the another object must be in rang. means that I have to aims on the object which is in the rang of Raycast. means that the another object must be touched with Raycast ray. means that my shooter will only shoot when the another object is in touch with Raycast Ray. thnx
I'm still confused about what you want to happen. Let me assume the Capsule is the target. And let me assume that enabling the CalButton is considered a hit. Do you care if the object is in front of this object? That is must the positive 'z' side of the object this script is attached to be facing the object in question for a hit to occur? If not, you don't need a Raycast. You could just do:
if (Vector3.Distance(transform.position, GameObject.Find("Capsule").transform.position) < 150) {
CalButton = true;
}
Again, the Debug.DrawRay() will tell you where you are casting your ray.
I did what you told me, it worked but what I figured out that the Debug.DrawRay() is not rotating with the camera rotation. I applied the script to camera. It produce the ray well, I how I am gonna rotate it. I also checked the unity documentation, which helps me a lot but I didn't find the solution. thnx for the help,
$$anonymous$$y script is right, but that rotating thingy is making it not workable. I simple just want that the Debug.DrawRay() rotates with the applied camera. $$anonymous$$y question is clear now. What I want that when the DrawRay hit the capsule it will activate the button. All my codes are ready to go except that rotating thingy... thnx for the help