Buttons in Canvas render mode "world space" not working

Hello Friends,

I as using unity 5.3.0f4. I am using a button where the canvas render mode is “World Space”. The main camera is revolving around an object and I want that button to always lookAt the camera. When I am using the below code the button is not working.

using UnityEngine;
using System.Collections;

public class HotspotLookAtCamera : MonoBehaviour {

public Transform target;

// Update is called once per frame
void Update () 
{
	transform.LookAt (target);
}

}

and in the inspector panel I am dropping main camera object in the target field. Please tell the solution. I have also selected the “Event Camera” as my Main Camera in inspector panel.

it is happening because canvas face is towards camera after applying your script and in such condition button rotate in exact opposite angle so to avoid this you can rotate your button manually to 180 in y axis.
Because button can’t be pressed from behind.
I have tried your script and just make my button angle to 180 in y-axis and it works. Hope it will be helpful for you.

Hello Rajan,

Thanks for your answer. I am using two scrips one to lookAt the camera and the other to trigger event on click. As you are saying a button can’t be clicked from behind, u r right. But the problem is the button is rotating and it’s facing the camera so there is no question of behind clicking. In this case please tell me what shall I do. The butto is follwing the camera movements but failing to work on the onclick button event. Please let me if you did not understand my doubt.

thanks,

@rajan4uto
The below script is attached to the button (i.e Unity icon)in world space:
using UnityEngine;
using System.Collections;

public class HotspotLookAtCamera : MonoBehaviour {

// Update is called once per frame
void Update() 
{
	transform.LookAt(Camera.main.transform.position, Vector3.up);
}

}

The below script is attached to the main camera
using UnityEngine;
using System.Collections;

public class CameraRotateMainScene : MonoBehaviour
{

public Transform target;
int degrees = 5;
public float dragSpeed = 2;
private Vector3 dragOrigin;

// Update is called once per frame
void Update () 
{
	
	if (Input.GetMouseButton (0)) 
	{	
		transform.RotateAround (target.position, Vector3.up, Input.GetAxis ("Mouse X")* dragSpeed);
		transform.RotateAround (target.position, Vector3.left, Input.GetAxis ("Mouse Y")* dragSpeed);

	}
	else
	{
		if(!Input.GetMouseButton(0))
			transform.RotateAround (target.position, Vector3.down, degrees * Time.deltaTime);	
	}	
}

}

Now my problem is :

  1. The button is always facing the camera but the click is not working
  2. I want the lookAt function and onclick to work together but its not working.
  3. Either the LookAt or the button is working.

Thanks,