Player Follow Camera Goes Inside Objects - unity3d

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;
}
}

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.

Camera follow player with procedural generation

Hello i wonder how can i implement camera what follows player in my game. Situation is following - first level is generated - then player is instantiated somewhere in this level then i want camera to follow player to do so - camera needs to have a target that it will follow.. but when game starts camera can not locate player - because level is still generating... so u cant implement click to move mechanic with camera cos camera has no target... what is the best way to make it work? assuming its a 3d top down game where player movement is depend on camera cos its a click to move movement scheme..
If you have a script that is doingthe instantiation of the player, could you not have it call the Camera movement script and give it the player as a target? Then you could just make sure in the Camera script that you must have a target to move.
Impliment a callback to be invoked after your level has generated.
In your camera controller
[serialisedFeild] private PlayerInstansiator _playerInstanciator; // assign this
private Player _player;
void OnEnable()
{
_playerInstanciator.OnPlayerCreated += AssignPlayer;
}
private void AssignPlayer(Player player)
{
_player = player;
}
In the class which creates the player, something like
public event Action<Player> OnPlayerCreated;
private CreatePlayer()
{
var player = Instanciate(playerPrefab);
OnPlayerCreated?.Invoke(player);
}

Unity 2D GameObject(s) Rotate Randomly

Good evening everyone,
I hate to ask this question but I am new to Unity and I'm running into an error(I think) that is really unexpected. I actually just started work on my first 2D game tonight and cannot find anything online that seems to explain this.
Long story short, I have a character (GameObject) that simply moves from the left to the right on a platform, then he should fall (or jump) onto the next platform and keep moving until he falls into infinity. I have been able to get my character to move from the left to the right, but when he reaches the midway point between the two platforms ... the platforms begin to rotate counter-clockwise. I'm including screenshots below to show what I mean. Has anyone seen this before?
My player has the RigidBody2D and Box Collider 2D Components
My platforms have the Box Collider 2D Components
This is the code I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControls : MonoBehaviour
{
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = new Vector2(1, rb.velocity.y);
if(Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector2(rb.velocity.x, 10);
}
}
}
and these are the photos of the game as it plays out:
The problem of your game isn't the character but the platforms!
On each platform you have a Rigidbody2D and a box collider 2D so the player can walk on them and collide.
On unity physics is always working and so even if you set the gravity to zero the player has gravity and so it will push the platforms and make them rotate
You can easily fix this by going to the constraints of the rigidbody2D of each platform and check the freeze rotation on the Z axis. It will stop rotating
Actually the platforms don't move...
But you have connect the camera Transform to the player transform; So when the play rotation changed(When he falls) The camera rotation changed with the player too
You can fix this issue by making the camera follow the player position via code, This video could help you

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.

Two Cameras need them to face each other

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.