Camera rotation within a prefab - unity3d

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.

Related

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

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

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.

Capsule Collider doesn't move smoothly

I have some issues using CapsuleCollider and RigidBodyController in a Player GameObject in Unity.
Using a CapsuleCollider as collider for the player, I noticed that the player doesn't move like a capsule or a sphere (in other words, I want that the movement should be like a rolling ball, with no stuttering), but its movements like more like a box pushed, with stops and starts, that make some little oscillations of the camera.
Any ideas, solutions or tips?
realy that is all based on how your moving the object, and the rigidbody2D settings of the object, I think using physics forces would roll your character like a ball or capsule, however just setting a velocity wont.

Unity3d with vuforia showing 2d image when targed is detected

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d.
The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is
detected. The original image on editor is placed then so far that it's difficult to find it.
I would be grateful if somebody can highlight me the right direction.
I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.
To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.
These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs
If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.
However, in case you want to do something more, here's some edited code.
DefaultTrackableEventHandler.cs
//Assign this in the inspector. This is the GameObject that
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;
Add the below lines to OnTrackingFound
//Enable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Found. Note that you can change the type of
//collider to be more specific (such as BoxCollider2D)
spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
spriteGameObject.GetComponent<Collider2D>().enabled = true;
//EDIT 1
//Have the sprite inherit the position and rotation of the Image
spriteGameObject.transform.position = transform.position;
spriteGameObject.transform.rotation = transform.rotation;
And the below to OnTrackingLost
//Disable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Lost.
spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
spriteGameObject.GetComponent<Collider2D>().enabled = false;
Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.
Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton
Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.
public class HandleClicks : MonoBehaviour {
//Event fired when a collider receives a mouse down
//and mouse up event, like the interaction with a button
void OnMouseUpAsButton () {
//Do whatever you want to
Application.LoadLevel("myOtherLevel");
}
}