The question is answered, right answer was accepted
Raycasting Stopped Working all of a sudden
Hello, i am using a script to select units with raycasting. While the script used to work just fine. All of a sudden it stopped hitting both the terrain and the objects. I have many objects with colliders on them. I haven't changed the lenght of the ray, nor the camera position. I am checking in the code whether the name of the object that's hit is Archer or not. But even if i remove that condition. The Position of the object hit is always 0,0,0. There's nothing in front of the camera. The y value of my colliders are too high.20 of x value is equal to my 17.500 of y value on colliders only.. Don't know what causes them to shrink so much. Edit: I figured it only works if the collider is long enough towards the camera, but then i have to make it gigantic. I tried using mathf.Infinity Please help me. Here is the code that uses raycasting.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UnitSelectArcher : MonoBehaviour {
Ray ourRay;
RaycastHit hit;
public Camera MainCamera;
public bool unitSelected=false;
public Button MoveButton;
public Button AttackButton;
public Canvas ActionMenu;
public string unitName="Archer";
float targetX;
float targetZ;
bool Moving=false;
public float speed=2f;
public GameObject kup;
public Transform spawnPoint;
// Use this for initialization
void Start () {
MainCamera = MainCamera.GetComponent<Camera> ();
MoveButton = MoveButton.GetComponent<Button> ();
AttackButton = AttackButton.GetComponent<Button> ();
ActionMenu.enabled = false;
targetX=transform.position.x;
targetZ = transform.position.z;
}
// Update is called once per frame
void Update () {
ourRay = MainCamera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ourRay, out hit, 100)
&& Input.GetMouseButtonDown (0)
//&& hit.collider.gameObject.name == "Archer"
) {
unitSelected = true;
Debug.Log ("{0}", hit.transform);
}
if (unitSelected == true) {
ActionMenu.enabled = true;
}
MoveButton.onClick.AddListener (MoveButton_Clicked);
// The Object clicked on moves to the Target Location
if (Input.GetMouseButtonDown (0) && Moving == true) {
targetX = hit.transform.position.x;
targetZ = hit.transform.position.z;
}
/* Deneme aşamasında ama yemiyor
if (Input.GetMouseButtonDown (0)) {
if (hit.collider.transform.name == name)
unitSelected = true;
else {
unitSelected = false;
Moving = false;
}
}
*/
if (Moving == true ) {
//Debug.Log (string.Format ("targetX:{0}, targetZ:{1}", targetX, targetZ));
if (targetX > transform.position.x)
{
Vector3 temp = new Vector3 (0.1f, 0, 0);
transform.position += temp;
}
else if (targetX < transform.position.x)
{
Vector3 temp = new Vector3 (0.1f, 0, 0);
transform.position -= temp;
}
if (targetZ > transform.position.z) {
Vector3 temp = new Vector3 (0, 0, 0.1f);
transform.position += temp;
}
else if (targetZ < transform.position.z)
{
Vector3 temp = new Vector3 (0, 0, 0.1f);
transform.position -= temp;
}
unitSelected = false;
}
}
public void MoveButton_Clicked()
{
Moving = true;
}
}
What about checking for mouse input and raycasthit separately?
So change it to:
if (Input.Get$$anonymous$$ouseButtonDown(0)){
ourRay = $$anonymous$$ainCamera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ourRay, out hit, $$anonymous$$athf.Infinity)){
Debug.Log ("{0}", hit.transform.gameObject.name);
if (hit.transform.gameObject.name == "Archer"){
unitSelected = true;
}
}
}
I'm not sure if this will help at all, but I think breaking it into separate events will help find the problem.
NonBreaker
I solved the problem by copying everything in the scene to a new scene. I think there had been some kind of bug with that scene.
Follow this Question
Related Questions
Raycast on moving object? 2 Answers
How i can fix a player controller bug? 0 Answers
2D collision trouble, character falling through map 1 Answer
Objects wont stop bouncing if they fall. 0 Answers
Problem with DontGoThroughThings script 0 Answers