Two Cameras need them to face each other - unity3d

I have two cameras in a scene,both following different objects I want to make them face each other ,like split screen but opposite sides,also I don't want to touch the rotation of the camera,any heads up with playing with cameras and split screening would be a great help,thanks!

This may be the thing you are looking for
// This complete script can be attached to a camera to make it
// continuously point at another object.
// The target variable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

Found an easy way,created an empty gameObject rotated it upside down and inserted the second camera in it,still any other solutions are welcome and tips for multiplayer on same phone would also be great help,thanks!
Edit:This does not work.

Related

How do get a 2D sprite to face the camera in a 3D unity game?

I've been trying to get it to work but most of the codes are either outdated or doesn't work can anyone help i need a code for unity 2021 ?
I tried finding some codes but they are pretty old like from 2016
The term "billboard" in computer graphics refers to an object that always faces the camera. You can try a billboard component, like so:
public class Billboard : MonoBehaviour {
public Transform cam;
private void Start() {
cam = Camera.main.transform;
}
void LateUpdate() {
transform.LookAt(transform.position + cam.forward);
}
}
A follow camera behaviour should always be implemented in the LateUpdate because it tracks objects that might have moved inside the Update. Also if your sprite is inside a canvas make sure its in world's space, so that it is a 3D world object and it can rotatute. Canvas space is an option in the canvas component itself.

Player Follow Camera Goes Inside Objects

I was working on a 3d game where the camera is continuously following the player object in position and rotation fields.
Complete 3d environment is set up as per my gameplay. A player moves and rotates in different areas of the environment, I was getting this kind of abnormal view in front of the screen. The camera goes into the other objects.
This is a really poor gameplay experience so how to fix this?
How to make this look better when the player following the camera goes inside other environment objects?
You can make an empty gameobject inside the player, then your camera script should not follow the player, but it should follow the empty gameobject instead.
if you define an offset, it would be best practice
camera script is as below:
public class Followplayer : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update () {
transform.position = player.position + offset;
}
}

There is no reason for this Unity script to make the sprite move, yet it moves

Here's the code:
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float strength = 100f;
void Start () {
//Initialize the body of the sprite so that forces
//can be applied.
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
//Note2Self: var says this is a variable of unspecified type.
var touch = new Touch();
/*
if (touch.phase == TouchPhase.Began){
rb.AddForce(transform.forward * strength);
}*/
if (Input.anyKey)
rb.position.Set(0, 100);
}
}
I was trying to practice some basic stuff in Unity (I am not used to programming in IDE's at all, we've just used vim in my program so far) when I happened upon this oddity.
First, I didn't understand why the sprite would move at all when there can't be a touch identification, since I haven't actually tested this on a mobile device. So I commented it out and for some reason the sprite still moves. That code shouldn't be doing anything, yet it is.
I have checked to see if the sprite is using the up-to-date script - it is - and I have checked if the script is targeting the correct rigid body and that it is a rigidbody2D. It is.
What is going on?
If it is just falling it is probably effected by gravity.
You can turn this off in your script by adding rb.gravityScale = 0; at the end of your Start() function
OR
by setting it in the editor inside the rigid body component
I looked in the unity documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
which says that applying a Rigidbody2D component to an object will place it under control of the physics engine.
The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces.
I have run into issues with rigidbodies on more than one occasion, I suggest you check the RigidBody2D component in the unity inspector window and make sure to uncheck use gravity.
Also, you may want to just write a custom script without using a rigidbody. Doing a search on youtube will probably give you exactly what you need for that. Hope this helps!

Camera rotation within a prefab

I am trying to create a multiplayer game in unity3d using a diablo like camera system. So wherever I click on the screen. The system it was working fine in singleplayer where I did not need to include a camera in a player prefab. However Now I am facing the problem where my camera rotation is also affected by the rotation of my prefab parent. The hierarchy looks like this:
There is a script added to the camera that looks like this:
using UnityEngine;
using System.Collections;
public class MainCameraComponent : MonoBehaviour {
public GameObject Reaper;
private Vector3 offset;
void Start () {
offset = transform.position - Reaper.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = Reaper.transform.position + offset;
}
}
When I run the game the camera always stays behind my character, while I want it to always stay at the same rotation. so if I order my character to walk north I would see his back, if it would walk south I wanted to see the front.
notice how the shadow changes, (thus the rotation) but i always face the back of my model. TLDR: I want my child camera to ignore the rotational change of its parent and be static. whilst letting my camera position be guided by its parent. as far as I know it seems impossible to make a networkmanager instantiate a new player prefab and attach a camera afterwards on the same hierarchy level.
Any help would be greatly appreciated.
However Now I am facing the problem where my camera rotation is also
affected by the rotation of my prefab parent
That's because your Camera is a child of the player so it will do whatever the Player/parent is doing.
Your camera should not be under the player. Remove it and make sure it is not a child of the Player or any other Object. That script should work or make the camera follow the player without making the Camera the child of the GameObject.
Remove Player = GameObject.Find ("Player"); from the LateUpdate function. You have already done this in the Start function, so it is unnecessary to do it in the LateUpdate function. GameObject.Find ("Player"); should never be called directly from the LateUpdate function. It will slow down your game.
EDIT:
After reading your comment, it looks like you want to instantiate a player with the camera. You can do this without making the camera a child of your player.
Your current Setup:
Player
Model
Projectile
Projectile Mesh
Camera
So your camera is under Player.
You new Setup:
PlayerPrefab
Player
Model
Projectile
Projectile Mesh
Camera (Attach script in question to this)
Camera is now PlayerPrefab instead of Player. Now, you can instantiate PlayerPrefab and move the Player with a script. I don't know what your move script looks like but it should be attached to the Player not PlayerPrefab. The PlayerPrefab is used to hold everything so that it can be easily instantiated as one. The code in your question should be attached to your Camera.

Unity3d 3d skybox not quite right

I'm messing around in unity3d in order to learn it.
Had a crack at making my own 3d skybox like in source engine for example. I'm using the standard 1st person controller.
I made another camera with same FOV for my skybox, and slaved it to the camera in the 1st person controller using the script below which I put on my skybox camera.
(Maincam field has the 1st person controller camera component in it)
using UnityEngine;
using System.Collections;
public class CameraSlave : MonoBehaviour {
public Component Maincam;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.rotation = Maincam.transform.rotation;
}
}
You can see result here. Its a bit funny. (The big tetrahedron shape in the background is in my skybox everything else is normal)
As far as I understand it as long as the camera fov is the same it doesnt matter what size my skybox things are.
I think the problem, is there is some lag maybe? Like the Update in the code above is being called one frame too late? I tried calling that update from the 1st person controllers mouse look script but as well as getting loads of errors the result was the same.
I can't visualize your example, btw:
I think the problem, is there is some lag maybe? Like the Update in
the code above is being called one frame too late? I tried calling
that update from the 1st person controllers mouse look script but as
well as getting loads of errors the result was the same.
You can't rely on the order in which Update method will be called by the engine (unless you force a particular order, but this isn't generally a good choice). For camera update operations it's better to use LateUpdate. It's guaranteed that it will be called after all Update methods.