I have this class that I want to get it’s location in another class.
This is the one I want to get:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private static PlayerMovement _Instance;
public static PlayerMovement Instance { get { return _Instance; } }
private CharacterController controller;
private Vector3 rotation;
private Vector3 ForMove;
Quaternion unused;
public GameObject compas;
public float GlobalX, GlobalZ;
public bool moving = false;
private float rotdir = 0;
private float compang = 0;
public float RotSpeed = 100;
public float ForSpeed = .5f;
private float RotAmount = 0;
private float ForAmount = 0;
public int direction = 0;
private Quaternion CompRot;
private bool goingForward = false;
void Awake()
{
Vector3 glpos;
glpos = transform.position;
GlobalX = glpos.x;
GlobalZ = glpos.z;
controller = GetComponent<CharacterController>();
}
void Update()
{
foreach (char c in Input.inputString)
{
}
if (!moving)
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
rotdir = -1;
direction = (direction + 3) % 4;
moving = true;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
rotdir = 1;
direction = (direction + 1) % 4;
moving = true;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
moving = true;
goingForward = true;
}
}
if (moving)
{
if (goingForward)
{
if (ForAmount + ForSpeed * Time.deltaTime >= 2)
{
ForMove = new Vector3(0, 0, 2 - ForAmount);
goingForward = false;
moving = false;
ForAmount = 0;
}
else
{
ForAmount += ForSpeed * Time.deltaTime;
ForMove = new Vector3(0, 0, ForSpeed * Time.deltaTime);
}
ForMove = this.transform.TransformDirection(ForMove);
controller.Move(ForMove);
}
else
{
if (RotAmount + RotSpeed * Time.deltaTime >= 90)
{
this.rotation = new Vector3(0, rotdir * (90 - RotAmount), 0);
compang += rotdir * (90 - RotAmount);
moving = false;
RotAmount = 0;
rotdir = 0;
}
else
{
this.rotation = new Vector3(0, rotdir * RotSpeed * Time.deltaTime, 0);
compang += rotdir * RotSpeed * Time.deltaTime;
RotAmount += RotSpeed * Time.deltaTime;
}
this.transform.Rotate(this.rotation);
CompRot = Quaternion.Euler(270, compang, 0);
compas.transform.localRotation = CompRot;
}
Vector3 glpos;
glpos = transform.position;
GlobalX = glpos.x;
GlobalZ = glpos.z;
}
}
}
This is one that I want to get the reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SelfDestroy : MonoBehaviour
{
public PlayerMovement ThePlayer;
void Update()
{
Vector3 selpos = transform.position;
if (ThePlayer == null)
{
return;
}
if (Math.Abs(selpos.z - ThePlayer.GlobalZ) > 10)
{
Destroy(this.gameObject);
}
}
}