- Home /
Question by
ToxicC00kie1001 · Feb 25, 2019 at 02:04 AM ·
raycastraycastingraycasthitrayraycasts
Raycast returns null for no apparent reason
Ok, so quick question, I made a simple:
Ray ray;
Then in the update() I made a simple:
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));
And for some reason, in the console, the debug.log registers a ray being casted while ray just thinks it's null.
Any ideas?
This is the debug.log output: https://i.stack.imgur.com/OpXFG.png
This is the ray output: https://i.stack.imgur.com/pmlex.png
In case I overlooked something, here's the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankController : MonoBehaviour
{
Vector3 targetPosition;
Vector3 lookAtTarget;
public Vector4 selectedColor;
public Vector4 deselectedColor;
Quaternion playerRot;
public float rotSpeed = 2;
public float speed = 3;
bool OneExecution = false;
int count = 0;
public bool Moving = false;
public bool Selected = false;
public bool UiSelected = false;
public bool Hovering = false;
public GameObject UI;
public BoxCollider HitBox;
public DoEverything Properties;
public GameObject childObj;
public MeshRenderer mrHighlight;
public PlayerMaster playerMaster;
int playerMasterTeam;
SkinnedMeshRenderer[] skinnedMeshRenderersScan;
public List<SkinnedMeshRenderer> skinnedMeshRenderersList = new List<SkinnedMeshRenderer>();
Ray ray;
RaycastHit hit;
void Start()
{
Properties = GetComponentInChildren<DoEverything>(); //Get the DoEverything script
childObj = Properties.InstancedEntity; //Get the object it will spawn
if (mrHighlight.enabled != false && mrHighlight != null) //Make sure the highlight isn't enabled and not null
{
mrHighlight.enabled = false;
}
skinnedMeshRenderersScan = childObj.GetComponentsInChildren<SkinnedMeshRenderer>(); //Looks for all skinned mesh renderers in child object
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
if (!skinnedMeshRenderersList.Contains(element)) //If it isn't already in this list
{
skinnedMeshRenderersList.Add(element); //Add to the list
}
}
playerMasterTeam = playerMaster.Team;
}
void LClickRay()
{
}
void RClickRay()
{
}
void OnMouseEnter()
{
Hovering = true;
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
element.material.color = selectedColor;
}
}
void OnMouseExit()
{
Hovering = false;
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
element.material.color = deselectedColor;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Physics.IgnoreCollision(collision.collider, HitBox);
}
}
void Move()
{
transform.rotation = Quaternion.Slerp(transform.rotation,
playerRot,
rotSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position,
targetPosition,
speed * Time.deltaTime);
if (transform.position == targetPosition)
{
Moving = false;
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
LClickRay();
}
if (Input.GetMouseButtonDown(1))
{
RClickRay();
}
if (Moving == true)
{
Move();
}
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));
}
}
Comment
Your answer
Follow this Question
Related Questions
Can you figure out raycast origin position from RacyastHit? 2 Answers
My raycast is ignoring my tilemap collider 0 Answers
Raycast detects box collider, but not capsule collider 0 Answers
RayCast2d Not colliding with objects in other layers 1 Answer
Play Animation on Raycast *open and close some objects* 0 Answers