Camera follow player with procedural generation - unity3d

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

Related

How do you play animation in unity when you walk through a trigger

I’m making a horror vr game in unity with the ovr player controller and I’m trying to figure out how to make an animation I made in blender play behind me when ever I walk into a certain box trigger.
You could use something along the lines:
[SerializeField] private GameObject player;
[SerializeField] private Animation animation;
private void OnTriggerEnter(Collision other)
{
if (other.gameObject == player)
animation.Play();
}
The player and animation fields need to be initialized from the inspector.

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

Make VR Camera Looks at Target Game Object

I'm making a VR Application in unity using steam VR, VRTK and HTC Vive. I have a simple Reset Button and what I want is, When I click on that reset button my VR Camera should turn towards the target Game Object and starts looking at it here is my code I use Transform. LookAt but it is not working.
Please help to solve this issue.
Thanks
using UnityEngine;
using System.Collections;
public class reset : MonoBehaviour
{
public Transform target;
public void resetview()
{
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
VR device override everytime the camera rotation of your Player RIG.
You need to disable your camera device and enable another camera outside of the VR player RIG and set how main camera.
Modify this camera instead of the VR camera RIG.

playing particle system in Unity

I am using Unity3D to develop for the HTV Vive using SteamVR. I have downloaded an asset from the asset store with explosion effect created using a particle system. I want to play the particle animation when an object is destroyed. Here is the code I am, unsuccessfully, using.
private void OnDestroy() {
explosion.GetComponent<ParticleSystem>().Play();
}
Explosion is a public variable of type GameObject set from the inspector. I drop the particle system object there.
Why is it not working? anyone a good recommendation on a short tutorial to learn to use (not to create) particle effects?
Thanks
view of the hierarchy
I have tried this with the PS as a child of the target and as an independent object.
view of the inspector (Target)
view of the inspector (particle system)
edit: for some reason, the particle effect is destroyed right after the scene starts.
Try making the explosion effect into a prefab and instantiate it when destroyed.
GameObject explosion; // Prefab asset
private void OnDestroy() {
Instantiate(explosion, transform.position, Quaternion.identity);
}
Also, don't forget the stop action to Destroy.

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.