- Home /
Question by
Sylon87 · May 16, 2019 at 02:37 PM ·
scripting problempositionscript.user interfacecamera viewport
Ui follow player fixed position
hello guys.
I have a player character that is moving in a plane with camera rotation setted to 60 degrees (not attached to the player characrer) I’m setting the UI position by taking player position put an offset and get the screen position from the camera so i’m able to make it always on player head, but because camera is in prospective when player is moving also the UI is mocing, it’s not really moving because i suppose that it is because camera is in prospective mode.
There is a way to make it look always in the same position?
here an example
here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SPC : MonoBehaviour
{
public GameObject image;
public Camera camea;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float xaxis = Input.GetAxis("Horizontal");
float yaxis = Input.GetAxis("Vertical");
Vector3 newPos = new Vector3(xaxis,0.0f,yaxis);
transform.Translate(newPos * 4.0f * Time.deltaTime);
UpdateImage();
}
public void UpdateImage()
{
Vector3 offset = new Vector3(0.0f,0.5f,0.0f);
Vector3 fixedPos = transform.position + offset;
Vector2 cameraScreenPos = camea.WorldToScreenPoint(fixedPos);
image.transform.position = cameraScreenPos;
}
}
Comment