i need some help in making my raycast be able to both activate animations and to pickup stuff.
Here is the script i need help with:
using UnityEngine;
public class PlayerRaycasting : MonoBehaviour {
public float distanceToSee;
RaycastHit whatIHit;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Debug.DrawRay (this.transform.position, this.transform.forward * distanceToSee, Color.magenta);
if (Physics.Raycast (this.transform.position, this.transform.forward, out whatIHit, distanceToSee))
{
if (Input.GetKeyDown (KeyCode.Space))
{
whatIHit.collider.gameObject.GetComponent<Animator>().Play("Activate");
}
/*if (Input.GetKeyDown (KeyCode.Space))
{
if (whatIHit.collider.tag == "Key")
{
if (whatIHit.collider.gameObject.GetComponent<Keys> ().whatKeyAmI == Keys.Keycards.goldKey)
{
Destroy (whatIHit.collider.gameObject);
}
}
}*/
}
}
}
The commented out piece is needed to be able to pickup a key.
The key is located inside a safe that you need to open.
But the safe’s collider that triggers the animation to open the door is interfering with the key pickup.
It tries to activate a animator animation on the key, which just needs to be destroyed (picked up with a bool).
Please help?
One thought when reading your code is that you might want to raycast only if the space key is pressed… that way it’ll be doing many less raycasts when not needed.
As for your issue, if the animation is playing okay, but doesn’t move out of the way so you can get the key, maybe you can disable its collider if that’s an option?
Or you could put code that just picks up the key if you know it’s in the safe, already and don’t require further input…
If for any reason disabling the collider isn’t possible, maybe use RaycastAll instead and if the first is the door and the second is the key, then pick it up…
Wow, fast reply
Yes, ive tried turning off the safe’s collider in the the animation, but it is still in the way somehow. If i take out the key from the safe, it works.
Isnt there some way to put a layermask for this?
Ive just started to read about layermasks.
But how would i go about doing that?
If the safe is under layermask 8 as in opening
And the key is under layer 9 as in pickups?
isnt there someway in the script i have that can be altered into it ignoring the safe collider and activate the animation function when it has hit the keys collider?
Oh, sure, ya just raycast on the layer of the keys or use the raycastall thing i mentioned earlier, and if within your distance you hit the keys, then play the animation (in either case).