Fixed
I'm trying to acess a child of the childs of an object for using them.
Hello, my probleme is that i'm trying to use multiple childs called "CameraFollower" of a childs of a game Objec called "PlayerSkins". this manipulation i want to do is for having a single camera for multiple player controllers or skins by accessing an object child of the player skin used as a target for the camera as u can see in the script down bellow. The script work well for a single object but i'm not able to take the objects i want and insert it on an array for using it. Sorry for my bad english...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public float CameraMoveSpeed = 120.0f;
public GameObject[] CameraFollowObj;
public GameObject test;
public float clampAngle = 80.0f;
public float inputSensitivity = 150.0f;
private GameObject[] PlayerList;
private Transform Ftarget;
private float mouseX;
private float mouseY;
private float finalInputX;
private float finalInputZ;
private float rotY = 0.0f;
private float rotX = 0.0f;
private float oldMousSensitivity;
void Start () {
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
oldMousSensitivity = inputSensitivity;
PlayerList = new GameObject[GameObject.Find("PlayerSkins").transform.childCount];
test = GameObject.FindGameObjectWithTag("CameraFollower");
for (int i = 0; i < GameObject.Find("PlayerSkins").transform.childCount; i++)
{
PlayerList[i] = GameObject.Find("PlayerSkins").transform.GetChild(i).gameObject;
}
foreach (GameObject Go in CameraFollowObj)
{
GameObject temp = Go;
for (int i = 0; i < CameraFollowObj.Length; i++)
{
temp = GameObject.FindGameObjectWithTag("CameraFollower");
}
}
}
void Update () {
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
finalInputX = mouseX;
finalInputZ = mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
void LateUpdate () {
CameraUpdater ();
RotatePlayerWithMouse();
}
void CameraUpdater() {
foreach (GameObject go in CameraFollowObj)
{
Transform target = go.transform;
Ftarget = target;
}
Transform AFtarget = Ftarget.transform;
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, AFtarget.transform.position, step);
}
public void RotatePlayerWithMouse()
{
if (!PauseMenu.GameIsPaused)
{
foreach (GameObject go in PlayerList)
{
go.transform.Rotate(0, mouseX * inputSensitivity * Time.deltaTime, 0);
}
inputSensitivity = oldMousSensitivity;
}
else
{
inputSensitivity = 0;
}
}
}
Follow this Question
Related Questions
Center of a GameObject with Bounds? 1 Answer
Destroy one child (of same instance) after another? 0 Answers
OnTriggerEnter2D works without RigidBody BUT only being in children 0 Answers
Show a text in the position of a GameObject 0 Answers
C# GameObject disabled scripts of its children after SetActive(false) 1 Answer