Problem with showing unknown (?) Cursor

after I implemented delegate to cameraRaycaster walkable and enemy Cursor has been shown but Unknown Cursor not showned ?

Can you attach your scripts codes, and also a snapshot of your editor window so we can see what cursor sprites you have assigned and where?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CameraRaycaster))]
public class CursorAffordance: MonoBehaviour {

	CameraRaycaster cameraRaycaster;
	[SerializeField] Texture2D walkcursor=null;
	[SerializeField] Texture2D enemycursor=null;
	[SerializeField] Texture2D outcursor=null;
	public Vector2 hotspot=new Vector2(0,0);//cursor boyutlar .örnek  new vector2(96,96)=99 piksel boyutunda .


	void Start () {
		cameraRaycaster = GetComponent<CameraRaycaster> ();	
		cameraRaycaster.onLayerChange += OnLayerChanged;//   Registering OnLayerChanged i layerchanngeobservera (CAMERARAYCASTERDA)ekle
	
	}

	// Update is called once per frame
	void OnLayerChanged (Layer newLayer) {
		print("layer over new layer");
		switch (newLayer) {
		case Layer.Walkable:
			Cursor.SetCursor (walkcursor, hotspot, CursorMode.Auto);
			break;
		case Layer.Enemy:
			Cursor.SetCursor (enemycursor, hotspot, CursorMode.Auto);
			break;
		case Layer.RaycastEndStop:
			Cursor.SetCursor (outcursor, hotspot, CursorMode.Auto);
			break;
		default:
			print ("nothing happened");
			return;
		}

			

		
		
	}
	//TODO consider de-registering  OnLayerChanged on leaving all game scenes

}

resim

not showing cursor . walkable and enemy cursors working but when hovering out of ground showing walkable
cursor again.

Can you post you CameraRaycaster.cs script also please.


See also;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRaycaster : MonoBehaviour {
	public Layer[] layerPriorities = {
		Layer.Enemy,
		Layer.Walkable
	};
	float distanceToBackground = 100f;
	Camera viewCamera;
	//-------------------------------------
	RaycastHit raycastHit;//içerden kullanım
	public RaycastHit hit//dışardan ulaşım için .PlayerMovement kullanıyor
	{
		get {  return raycastHit; }
	}
	//--------------------------------------
	Layer layerHit;//bu  scriptte bunu kullanıyoruz.içerden kullanım için
	public Layer currentLayerHit   //dışardan ulaşım için  playermovement ,cursoraffordance
	{
		get { return  layerHit; }
	}
	//--------------------------------------------

	public delegate void OnLayerChange(Layer newLayer); // declare new delegate type
    public event OnLayerChange onLayerChange; // instantiate an observer set

      void Start()
      {
	viewCamera = Camera.main;

	  }

		 void Update(){
			foreach (Layer layer in layerPriorities)
			{
			var hit = RaycastForLayer(layer);//herbir layer için hit o layer için hittir.(hit i layer a göre tanımlamak.)public hit

	if (hit.HasValue)
	{
		             raycastHit = hit.Value;//hitin değeri.içerden kullanım raycastHit.hit=public
		                if (layerHit != layer) // if layer has changed.eğer o layer değilse
			                {
			                    layerHit = layer;//bu layerdır.nonpublic 
		                    onLayerChange(layer); // call the delegates
			               }
		layerHit = layer;//non public
		return;
	   }
		}//yada 
		 raycastHit.distance = distanceToBackground;//raycastHit nonpublic
		layerHit = Layer.RaycastEndStop;
		}
	RaycastHit? RaycastForLayer(Layer layer)//? DEMEK NULLABLE DEMEK
	{
		int layerMask = 1 << (int)layer; // See Unity docs for mask formation
		Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);

		RaycastHit hit; // used as an out parameter
		bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
		if (hasHit)//EĞER BiŞEYE DEĞDiYSE
		{
			return hit;//HiT=RAYCASTHiT
		}
		return null;//YADA HiT=NULL
	}
}

…and your Utility.cs file please.

public enum Layer {
	Walkable = 8,
	Enemy =  9,
	RaycastEndStop=-1
}

From what I can see, you have everything as it should be. I took a look at the video(s) in question and I note that Ben moves the cursor on the walkable terrain and then up to the menu and makes comment about how the unknown is now working, yet in the video this is not shown.

If you check the following section, you’ll note that there is a lecture titled, Fully event Based Raycasting (lecture 46). If I had to hazard a guess (because I honestly can’t remember my own experience with this), I would suggest that this issue is resolved in that lecture when the how camera raycasting / cursor affordance is overhauled anyway.

If you want to be certain, one thing you could do is download, from GitHub, the project as it is at the end of the 22 and see if, as a separate project, you have any different results with regards to the cursor. If not, e.g. it doesn’t work correctly in the course version either, I would suggest moving along with the course content until you get to lecture 46 where I expect it is resolved (most likely through the change to the default state in the switch statement.


Updated Mon Jun 04 2018 14:03

Just tested this myself, with the link below, couple of issues;

  • the three cursor types are not associated with the Main Camera GameObject
  • the three cursor types have the default, and incorrect, import settings. They need to be changed to Cursor and then have those changes applied (click Apply)
  • after doing the above, test the Sandbox scene, and you will note that the Unknown cursor doesn’t get set when the mouse is over the grey area.

So to confirm, your code is inline with the course version and this does get corrected later on in lecture 46.

Tagging @Ben with regards to the other issues for the cursors though.


See also;

finally @ben explained the cursor issue .if you look

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms