Show UI in front of player Unity VR - c#

I am trying to show a UI always in front of the player, and facing it, in a Unity3D VR project. I am using Unity 2021.3.5f1.
I have a simple UI: a Canvas, with a Panel and two TextMeshPro inside it. The Canvas is child of an empty GameObject, with a script in it that manages the UI. The hierarchy is as follows:
My aim is to always show the UI in front of the player, facing the player itself. For this reason I wrote this script:
using UnityEngine;
namespace UI
{
public class KpPanelManager : MonoBehaviour
{
[SerializeField] private Transform playerHead;
[SerializeField] private float spawnDistance = 2f;
[SerializeField] private float yOffset = 0f;
[SerializeField] private GameObject panel;
private Vector3 _playerHeadForward;
private void Awake()
{
_playerHeadForward = playerHead.forward;
}
private void Update()
{
// show the panel in front of the player
var position = playerHead.position;
panel.transform.position = position + new Vector3(_playerHeadForward.x, yOffset, _playerHeadForward.z).normalized * spawnDistance;
// rotate the panel to face the player frame by frame
panel.transform.LookAt(new Vector3(position.x, panel.transform.position.y, position.z));
panel.transform.forward *= -1;
}
}
}
However, it does not work properly: it is correctly in front of the player, but it does not follow the player's camera itself when it moves. In the Unity editor I am referencing the Canvas that contains my UI as the panel GameObject in script, and the MainCamera of the XROrigin as the playerHead Transform in the script.
Can anyone help me?

You could use Transform's TransformPoint() to covert position from local to world space
Something like following would work for your case
Vector3 offset = new Vector3(0,0,2); // 2 units in front of camera
panel.transform.position = playerHead.TransformPoint(offset);

Related

Struggling to create a trajectory prediction in Unity with 2D physics simulation

I have a 2D brick breaker style game in Unity where you get to choose the angle of your shot before each turn with the mouse. I'd like to show an accurate trajectory prediction of what a ball will do while the player is aiming, up to maybe 200 frames of ball movement (for now). This involves potentially bouncing off the top, left, and right walls, and any bricks.
I'm using a physics simulation and line renderer to accomplish this but can't get it to work correctly.
It's supposed to copy the gun, walls, and bricks into a virtual Scene, launch a ball in the angle the player is aiming, simulate 200 frames of movement, and plot those in a line renderer.
Here is an abridged version of the game code, reduced to just focus on the aiming and physics simulation for the 1st turn. When I run it, it simply plots the 200 points of the line renderer in the same coordinates as the gun.
What am I doing wrong? Thank you!
using UnityEngine;
using UnityEngine.SceneManagement;
public class Aim3 : MonoBehaviour
{
private Vector3 gunPosScreenV3, aimPointScreenV3,
aimPointWorldV3, aimDirectionV3;
private Vector2 ballDirection, ballVelocity;
[SerializeField] private GameObject gun, walls, bricks;
Camera cam;
LineRenderer lr;
// For Physics Scene
private int maxPhysicsFrameIterations = 200;
private Scene simulationScene;
private PhysicsScene2D physicsScene;
[SerializeField] private GameObject ballPrefab;
private GameObject ghostObj;
void Start()
{
lr = GetComponent<LineRenderer>();
lr.enabled = true;
cam = Camera.main;
CreatePhysicsScene();
}
void Update()
{
DrawTrajectory();
}
// This is run once to create a duplicate of the game board for the physics simulation
private void CreatePhysicsScene()
{
simulationScene = SceneManager.CreateScene("Simulation", new CreateSceneParameters(LocalPhysicsMode.Physics2D));
physicsScene = simulationScene.GetPhysicsScene2D();
// Copy the gun to the simulated scene
ghostObj = Instantiate(gun.gameObject, gun.transform.position, gun.transform.rotation);
ghostObj.GetComponent<SpriteRenderer>().enabled = false;
SceneManager.MoveGameObjectToScene(ghostObj, simulationScene);
// Copy all the bricks to the simulated scene. These are stored under a parent Game Object that
// is attached in the editor
foreach (Transform obj in bricks.transform)
{
ghostObj = Instantiate(obj.gameObject, obj.position, obj.rotation);
ghostObj.GetComponent<SpriteRenderer>().enabled = false;
SceneManager.MoveGameObjectToScene(ghostObj, simulationScene);
}
// Copy the top, left, and right walls to the simulated scene. These are invisible and don't have sprites.
// These are stored under a parent Game Object that is attached in the editor
foreach (Transform obj in walls.transform)
{
ghostObj = Instantiate(obj.gameObject, obj.position, obj.rotation);
SceneManager.MoveGameObjectToScene(ghostObj, simulationScene);
}
} // CreatePhysicsScene
private void DrawTrajectory()
{
// Get the starting Screen position of the gun from the World position
gunPosScreenV3 = cam.WorldToScreenPoint(gun.transform.position);
gunPosScreenV3.z = 1;
// Get position of mouse in screen units
aimPointScreenV3 = Input.mousePosition;
// Get the position of the mouse in world units for aiming
aimPointWorldV3 = cam.ScreenToWorldPoint(aimPointScreenV3);
aimPointWorldV3.z = 1;
// Calculate the normalized direction of the aim point compared to the gun
aimDirectionV3 = (aimPointScreenV3 - gunPosScreenV3).normalized;
Physics2D.simulationMode = SimulationMode2D.Script;
// Instantiate the ball prefab for the simulation
ghostObj = Instantiate(ballPrefab, gun.transform.position, Quaternion.identity);
// Assign the ball's velocity
ballDirection = new Vector2(aimDirectionV3.x, aimDirectionV3.y);
ballVelocity = ballDirection * 20f;
ghostObj.GetComponent<Rigidbody2D>().velocity = ballVelocity;
SceneManager.MoveGameObjectToScene(ghostObj.gameObject, simulationScene);
lr.positionCount = maxPhysicsFrameIterations;
for (var i = 0; i < maxPhysicsFrameIterations; i++)
{
physicsScene.Simulate(Time.fixedDeltaTime);
//Physics2D.Simulate(Time.fixedDeltaTime);
lr.SetPosition(i, ghostObj.transform.position);
}
Destroy(ghostObj);
Physics2D.simulationMode = SimulationMode2D.Update;
} // DrawTrajectory
}
You use aimPointScreenV3 but it appears to never be initialized/calculated.
Found the answer. Just needed to add the velocity to the ball after moving it to the simulated scene.
SceneManager.MoveGameObjectToScene(ghostObj.gameObject, simulationScene);
ghostObj.GetComponent<Rigidbody2D>().velocity = ballVelocity;

Sprite object disappears from the game tab after repositioning with script

I'm new to Unity so this can be an amateur mistake.
I'm trying to create a simple game that moves a simple sprite circle to a random position upon receiving touch input.
the program works well in Scene tab, but the circle fully disappears in game tab and the connected remote device after receiving touch input.
Here's the script I have connected to my sprite circle Object :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
private Touch theTouch;
private Camera cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
theTouch = Input.GetTouch(0);
if (theTouch.phase == TouchPhase.Ended)
{
MoveToRandomePosition();
}
}
}
// Random Movement
void MoveToRandomePosition()
{
var position = cam.ScreenToWorldPoint(new Vector2(Random.Range(0, Screen.width), Random.Range(0, Screen.height)));
transform.position = position;
}
}
You may wanna check the Z coord of your camera and circle. If you're spawning it behind the camera, it wont render.
You could do something like position.z = 0 if your cam pos is at the default -10.

Unity OnBecameInvisible() fires though object is still visible

I have a Mesh Renderer and a script assigned to a rotating sphere with a hole in it. The sphere has no specific or special place in hierarchy, its just next to the camera. The script part looks like this:
void OnBecameInvisible() {
Destroy(gameObject);
}
Problem is, that when I pass the sphere with my ball, even though the sphere is still half visible, it gets deleted. I have no other camera in the scene, and the one Im using is marked as the main camera.
Video
Instead of using OnBecameInvisible for culling objects you've passed, just check if it's sufficiently behind the camera in Update:
Camera mainCam;
[SerializeField] float maxBehindDistance = 0.5f;
void Awake() { mainCam = Camera.main; }
void Update()
{
Vector3 relPos = mainCam.transform.InverseTransformPoint(transform.position);
if (relPos.z < -maxBehindDistance)
{
Destroy(gameObject);
}
}

How to make Camera follow only the horizontal movement (x axis) of the player?

I am a super newbie in Unity and am learning by making my first game. I want the camera to follow the player, but only in the X axis. I had earlier made the camera a child of the player, but that didn't work as I wanted. So I whipped up a C# script to follow the player, as given below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraFollow : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(GameObject.Find("robot_body").transform.position.x, 0f, 0f);
}
}
However, this is showing only the blue background when run. Am I doing something wrong?
By setting your z position to 0 your camera probably ends up to close to everything in order to render it.
Try to not overwrite y and z with 0 but rather keep the current values:
// If possible rather already reference this via the Inspector!
[SerializeField] private GameObject robot;
private void Start()
{
// As fallback get it only ONCE
if(!robot) robot = GameObject.Find("robot_body");
}
void Update()
{
// get the current position
var position = transform.position;
// overwrite only the X component
position.x = robot.transform.position.x;
// assign the new position back
transform.position = position;
}

Unity3D CharacterController movement issue. Camera position not changed

I have a problem that main camera position doesn't change after CharacterController.SimpleMove() called. The task is to create scene where camera moves.
I have Main Camera game object with Character Controller and Script attached.
The issue is that nothing in vrCamera position changed after SimpleMove() called.
My question is what is wrong in this code. I suggest something wrong with binding between MainCamera object and CharacterController component, but I have spend a lot of time investigating and nothing working found.
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class VRLookWalk : MonoBehaviour {
public Transform vrCamera;
public float toggleAngle = 30.0f;
public float speed = 3.0f;
public bool moveForwad;
private CharacterController cc;
// Use this for initialization
void Start () {
cc = vrCamera.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f)
{
Vector3 forward = vrCamera.TransformDirection(Vector3.forward);
cc.SimpleMove(forward * speed);
}
}
}
You can't move the VR Camera, it's the SDK that determine the mainCamera position.
In order to move your camera you can just make a new GameObject as a parent of your mainCamera then move the parent GameObject
Try this. Your TransformDirection probably returns wrong vector.
Vector3 forward = vrCamera.transform.forward;
cc.SimpleMove(forward * speed);

Categories