- Home /
Change Colider to Raycast instead?
Anyone out there know the solution, apparently its real hard to find lol. Right now I have to be standing on a colider to pick up the object. I would much rather just click on the object using a raycast, seems more natural for my kinda game. Suggestions?
using UnityEngine;
using System.Collections;
public class DoneKeyPickup : MonoBehaviour
{
public AudioClip keyGrab; // Audioclip to play when the key is picked up.
private GameObject player; // Reference to the player.
private DonePlayerInventory playerInventory; // Reference to the player's inventory.
void Awake ()
{
// Setting up the references.
player = GameObject.FindGameObjectWithTag(DoneTags.player);
playerInventory = player.GetComponent<DonePlayerInventory>();
}
void OnTriggerStay (Collider other)
{
// If the colliding gameobject is the player...
if(other.gameObject == player)
{
if(Input.GetButtonDown("Interact"))
{
// ... play the clip at the position of the key...
AudioSource.PlayClipAtPoint(keyGrab, transform.position);
// ... the player has a key ...
playerInventory.hasKey = true;
// ... and destroy this gameobject.
Destroy(gameObject);
}
}
}
}
You could use an:
void On$$anonymous$$ouseDown()
event, and send a ray cast from the mouse position out a set amount of units from the camera and if it hits the collider, then pick it up
Thanks bro, I'm new to scripting, I am unsure how to even write that lol
Did you figure it out? Sorry, I was busy when you got back. If you haven't figured it out yet, I can help you out. Just don't want to write the code if you already found a solution
hey man, sorry i didn't get an email saying you replied, no I haven't figured it out yet sadly, you got some ideas?
Answer by Romano · Feb 24, 2014 at 02:19 AM
I wrote a post a while ago explaining a little bit what's tricky about trying to do raycast clicks in 2D, there's also a script there that solves the issue:
http://notquiteblackandwhite.com/post/75474540217/how-to-do-2d-mouse-clicks-in-unity
Hope it helps!
It is a FPS so I guess that would be 3d not 2d. Would it work the same you think?
I'm an assclown, I totally read the word 2D from another question and thought it was this one, sorry...
This particular script only works in 2D but could be modified to use z depth ins$$anonymous$$d of sorting layers/order for 3D.
haha way to funny. We all have those days. If you can think of any solution to mod the script please let me know
Edit: Went to your website the game looks incredible, I wanna play it badlyyyyyy
Hehe, cheers. This guy outlines how to do it: http://answers.unity3d.com/questions/42384/raycasting-with-mouse-click-but-nothing-is-happeni.html
Need to make sure whatever you're trying to click on has a box collider component attached.
That would be great if I were using java haha I'm using C# in the code above
Answer by JeffreyD · Feb 28, 2014 at 07:00 PM
Hey Guys, Here is how I did a RayCast script placed on the camera. It's for MouseInput not Touch but touch is similar. Note you can check for the "tag" of the collision object or the "name" (As seen in the Hierarchy) either one works. Below uses name.
It's in C# but I think I have one kicking around (Similar but not the same) in JS if you need it. I did try this in 2D and it worked. Not sure if that is a correct application but it worked. I've been meaning to post a Q about that.
Anyway, here is also a link to discussion on Touch in 2D using raycast script.. http://answers.unity3d.com/questions/638075/how-to-destroy-object-by-touch.html
There are several types of Raycasts. The script below uses Physics.Raycast but perhaps Collider.Raycast would be better for your application. I'm not not sure I've not tried them all. Here is a link to the search for Raycast in the Script reference doc. http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=raycast
I have not tested this script below since I took out some stuff to show it to you. The vars could also be defined outside the update function. It all depends on what you're trying to do. Hope it helps.
using UnityEngine;
using System.Collections;
public class rayCastOnMouseDown : MonoBehaviour
{
void Update()
{
// If left mouse is down.
if (Input.GetMouseButtonDown(0))
{
Debug.Log ("LeftMouse is down");
RaycastHit hitInfo;
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(rayOrigin, out hitInfo, 20))
{
Debug.Log ("We've hit " + hitInfo.transform.Name);
if (hitInfo.transform.name == "MyTargetObject")
{
Debug.Log("We hit TARGET 1");
// .. do whatever you want to do to Target 1 Object...
}
if (hitInfo.transform.name == "MyOtherTargetObject")
{
Debug.Log("We hit TARGET 2");
// .. do whatever you want to do to Target 2 Object...
}
}
}
}
}
Thanks for the answer bro! I'm about to play around with it to see if it suits my game needs, basically I want to use the raycast for items that I can pick up in the level. Clicking on them seems nicer then having to be inside a collider, hope this works!! :)