Hi guys, I have found script from the internet that the GUI Texture follows the player. It is worked, but once I pressed W and S, the position of the GUI Texture is not top of the player like once I pressed A and D.
Below is the picture that I am talking about: (Notice that when the GUI Texture is not on the top of the player anymore, I have pressed W or S, the GUI Texture is moving to the left side once I have pressed W, and it is moving to the right side, once I have pressed S).
Here is the picture:
![1]
Here is the large picture (link):
Here is the code that I have found on the internet:
using UnityEngine;
using System.Collections;
public class IconFollow : MonoBehaviour
{
public Transform target; // Object that this label should follow
public RaycastHit hit;
public Vector3 offset = Vector3.up; // Units in world space to offset; 1 unit above object by default
public Camera cameraToUse; // Only use this if useMainCamera is false
public bool clampToScreen = false; // If true, label will be visible even if object is off screen
public float clampBorderSize = .05f; // How much viewport space to leave at the borders when a label is being clamped
public bool useMainCamera = true; // Use the camera tagged MainCamera
private Camera cam;
private Transform thisTransform;
private Transform camTransform;
private void Start()
{
thisTransform = transform;
if (useMainCamera)
{
cam = Camera.main;
}
else
{
cam = cameraToUse;
}
camTransform = cam.transform;
}
private void OnGUI()
{
if (Portal.onTriggered)
{
this.guiTexture.enabled = true;
ShowIcon();
}
else if (!Portal.onTriggered)
{
this.guiTexture.enabled = false;
}
}
private void ShowIcon()
{
if (target)
{
var relativePoint = cam.transform.InverseTransformPoint(target.position);
if (clampToScreen)
{
var relativePosition = camTransform.InverseTransformPoint(target.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);
thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize), Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize), thisTransform.position.z);
}
else
{
if (relativePoint.z > 0.0)
{
if (Physics.Raycast(cam.transform.position, (target.position - cam.transform.position), out hit, Mathf.Infinity))
{
if (hit.transform == target)
{
thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
this.guiTexture.enabled = true;
}
else
{
this.guiTexture.enabled = false;
}
}
}
}
}
else
{
Destroy(gameObject, 0.8f);
}
}
}
Below is the code that been using for the player movement:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))] // This script requires CharacterController attached to the game object where this script attached into
public class CheckPlayer : MonoBehaviour
{
private Vector3 moveDirection = Vector3.zero; // Define and set for the movement of the player is not moving by default
private float gravity = 20.0f, speed = 5.0f; // Define and set for the gravity, speed of the player
private void Update()
{
// Rotate the Player
transform.Rotate(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0);
Camera.main.transform.eulerAngles = new Vector3(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0);
// Get the CharacterController component
CharacterController controller = GetComponent<CharacterController>();
// If the character is on the ground
if (controller.isGrounded)
{
// Get the axis direction for the movement of the character from the Input in the editor
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// Player movement depends on the move direction
moveDirection = transform.TransformDirection(moveDirection);
// The player movement is depends on the player speed
moveDirection *= speed;
}
// How much for the time for player takes to hit the ground because of gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the character each second while pressing the key input defined in the editor
controller.Move(moveDirection * Time.deltaTime);
}
}
Your answer much appreciated!
Thank you