0
votes

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:

enter image description here

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.

enter image description here enter image description here

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.

1

1 Answers

1
votes

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.