I wrote this script to instantiate a character in one of three locations as marked by empty game objects randomly when I hit b while a ray is colliding with anything tagged enemy. MonoDevelop shows no errors but the ray doesn’t seem to be functioning and when I use debug.drawray I see no ray.
using UnityEngine;
using System.Collections;
public class RaycastMindJump : MonoBehaviour
{
public Transform[] mindSpawn = new Transform[3];
public GameObject mainCamera;
public GameObject mplayer;
public float mindRayLength;
public Transform GetWaypoint()
{
int randomIndex = Random.Range (0, mindSpawn.Length);
return mindSpawn [randomIndex];
}
public GameObject CreateMindSelf()
{
Transform whichmind = GetWaypoint();
GameObject mindPlayer = Instantiate(mplayer, whichmind.position,whichmind.rotation) as GameObject;
return mindPlayer;
}
void Update ()
{
if(Input.GetButtonDown("b"))
{
RaycastHit hit;
Ray mindRay = new Ray(transform.position,Vector3.forward);
Debug.DrawRay (transform.position, Vector3.forward);
if(Physics.Raycast(mindRay, out hit,mindRayLength))
{
if(hit.collider.tag == "Enemy")
{
mainCamera.SetActive(false);
CreateMindSelf();
}
}
}
}
}