guys I feel silly have been looking the whole day in unity answers but my script still doesn't work been trying to use gameobject method to link my shieldHP with mytest manager to see if the shield would actually get destroyed and also sot htat I may do this in the future with other scripts
Here they are
First script:
using UnityEngine;
using System.Collections;
public class LightFolow : MonoBehaviour {
public Object Shield;
public Camera cam;
private float MaxW;
public Rigidbody2D Light;
Vector3 targetPosistion;
public int LightHP;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
Light = GetComponent ();
Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
MaxW = targetWidth.x;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
Light.MovePosition (targetPosition);
if (LightHP == 0 || LightHP < 0)
Destroy (Shield);
}
}
and then :
using UnityEngine;
using System.Collections;
public class TestManager : MonoBehaviour {
GameObject Shield;
// Use this for initialization
void Start () {
LightFolow ShieldHealth = Shield.GetComponent ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode("K"))
ShieldHealth.LightHP - 20;
}
}
+ I would also like to do this for my shield and player since they bother have the same target position obviously so if someone could help me carry that value over would be great
Player script:
using UnityEngine;
using System.Collections;
public class PlayerChontrol : MonoBehaviour {
public Camera cam;
private float MaxW;
public Rigidbody2D Player;
Vector3 targetPosistion;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
Player = GetComponent ();
Vector3 UpperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (UpperCorner);
MaxW = targetWidth.x;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
Vector3 targetPosition = new Vector3 (rawPosition.x,rawPosition .y,0.0f);
float targetWidth = Mathf.Clamp (targetPosition.x, -MaxW, MaxW);
targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
Player.MovePosition (targetPosition);
}
}
Soz if the is bad I is nub and this is just a hobby but I would really like to learn more so pls help me :D
↧