I have got a sphere that moves in the world space with respect to the camera. I'm clamping an image to the sphere so that it moves together with the sphere. I'm using the following code to do so. This is attached to the sphere gameobject.
public class ClampImage : MonoBehaviour
{
public Camera FirstpersonCamera;
public GameObject image;
void Update()
{
//get the position of the sphere in the worldspace
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
//assign the world position of the sphere to the image
image.transform.position = spherePosition;
}
}
I tried to use the same code to clamp the Canvas itself to the sphere but it is not being clamped to the sphere. How do I clamp the entire canvas to the sphere?
Update:
entire canvas refers to the canvas together with its child Gameobjects like texts and images.
The canvas is within the PlayerGameobject. The following are the settings for the canvas and the player Gameobject.
Canvas setting:
Player Gameobject setting:
I have registered the player in place of the image. It just vanishes completely when I ran the code.
If you group the UI items you want in a panel, you can move the panel by placing it in your "image" variable as thats a GameObject, and then you can move the panel as required and all objects will move with it
Related
Currently I have 2 cameras, one for viewing the 3D objects (Perspective), and the other camera to view 2D objects (Orthographic).
(Also, the view of the camera never intercepts each other.)
I am trying to display a 2D object based on the position of a 3D object, like so:
What I have is the respective 2D and 3D Camera, as well as the Vector3 position of the 3D GameObject.
What I currently have:
public Vector2 Convert3DPositionTo2DPosition(Camera camera3D, Vector3 position3D, Camera camera2D) {
var tempPos = camera3D.WorldToViewportPoint(position3D);
return camera2D.ViewportToWorldPoint(tempPos);
}
The only problem is that the returned position is not completely aligned with the 3D position.
Which results in this kind of results:
Also, I have made sure that both the 3D and 2D object have their pivot point are set correctly in the center, but it still does not work as intended.
(I am currently using Unity 2019.1.14f1)
Edit
TLDR of what I want:
I want a world position that is based on an object in view of the 3D camera which is now in view of the 2D camera and have them look like they are in the same position in the player's point of view (display screen).
What I am going to instantiate in afterwards (using that position) is a non-UI GameObject.
More specifically a particle system.
Check the Viewport Rect settings for both of your Cameras!
if there is any difference between the two cameras you will always get an offset in the positions.
I tested your solution using
public Camera camera3d;
public Camera camera2d;
public Transform obj3d;
public Transform obj2d;
void Update()
{
obj2d.position = camera2d.ViewportToWorldPoint(camera3d.WorldToViewportPoint(obj3d.position));
}
and it just works fine - as long as the Viewport Rect settings match - as you can see here and here.
My gameobject is a rectangle. It has a rigidbody, and a boxcollider. The anchor point is toward the left end of the gameobject. Using this code while the game is running causes the gameobject to turn around the z axis on the anchor point. when i set istrigger false, the object begins spinning around the center of itself. why does this happen? Is there a way to make the gameobject to spin around its anchor point while the istrigger bool of the box collider is false?
After some playing around, i have figured out that it i rotating on the center of the boxcollider. I need to figure out how to change the center of the box collider now without affecting the shape
private void FixedUpdate()
{
rigidbody.AddTorque(Vector3.back);
}
rigidbody.centerOfMass = gameobject.transform.position;
I'm working on Vuforia application in Unity.
How can I make a 3D object, attached to the ImageTarget to always be vertical during the marker recognition?
So that if I will rotate the marker the model won't be upside down, but remain vertical. It should somehow "understand" were is the worlds z-axis, perhaps using sensors of the iPhone.
Here is an illustration of what I mean (grey square is a marker, a green guy is a 3D object, attached to it).
Currently if I will rotate the marker the model will also rotate:
I need to fix the rotation of the model along z-axis so that it always will remain vertical:
You can use Transfrom.LookAt () to make sure the object always camera facing, with Vector.Up as the 2nd argument:
using UnityEngine;
using System.Collections;
public class CameraFacing : MonoBehaviour
{
public Camera m_Camera;
void Update()
{
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
m_Camera.transform.rotation * Vector3.up);
}
}
Attach this script to the image target's child (which will be shown on tracking is found). The camera is the AR camera you added in the scene.
I am trying to make a game where the camera follows the user, as they move around. I made the camera a child of the player and in the editor, the camera rotates around the player fine. When I play the game (in Unity) and rotate the player, the camera literally rotates instead of rotating around the player to keep the same regular following distance.
I use transform.Rotate() to rotate the player by the way.
In Summary:
Looking for camera that follows player in 3D game, that does not look different when the player turns.
issue is that the camera appears in the editor to rotate around the player perfectly, but not when transform.Rotate() is called on it during runtime in Unity.
All help is appreciated, tyvm for help.
I made the camera a child of the player and in the editor
Everything went down by doing this. You don't make the camera a child if you want it to follow the player.
What you do is to get the distance between the camera and the player in the Start() function. This is also called offset. In the LateUpdate() function, continuously move the camera to the Player's position, then add that offset to the camera's position. It is as simple as that.
public class CameraMover: MonoBehaviour
{
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
mainCameraTransform.position = playerTransform.position + cameraOffset;
}
}
I'm using my camera as a child gameobject of my ball, so when I move my ball camera comes with him. But the problem is I'm using rigidbody.addForce(), so when ball rotates the camera rotates with it, too.
So what should I do not to rotate my camera but only move it with my ball?
void FixedUpdate()
{
rigidbody.addForce(Input.getAxis("Horizontal"), 0, Input.getAxis("vertical"));
}
There are several ways to solve this, I'll list a few.
The first, if you don't care if your ball rotates or not, you can just disable the rotation on the ball. If you open the Constraints section in the rigidbody component, you can freeze the rotation so that the ball won't rotate.
Alternatively, you can write a script that keeps the camera always oriented a certain way. Depending on if you want the camera to rotate around the ball on any plane depends on the way you would implement this.
The third option, which is cleanest, is to not have the camera be a child of the ball. A minimal component to do this would look like this:
public class TargetFollow : MonoBehaviour
{
public Transform Target;
public float DistanceFromTarget;
void Update()
{
transform.position = Target.position + new Vector3(0, 0, DistanceFromTarget);
transform.LookAt(Target);
}
}
Just drag your ball into the 'Target' slot in your component. Keep in mind, this is super bare bones. You may want to add more variables to better control the direction the camera should be from the ball, or perhaps something that grabs a snapshot of the direction and distance the camera is from the ball in the Start method.