I have created a script for a homing missile that chases you and damages you once you get hit. It works fine in the editor, but after I build it, my character does not take any damage at all.
(The missile is located in a prefab).
This is my player script:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Floats
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
//Booleans
public bool grounded;
public bool canDoubleJump;
//Stats
public float curHealth;
public float max_health = 100f;
//References
private Rigidbody2D rb2d;
private PlayerHealthSystem phs;
public Transform fp;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
phs = gameObject.GetComponent<PlayerHealthSystem>();
fp = this.gameObject.transform.GetChild(3);
curHealth = max_health;
}
void Update()
{
if (Input.GetAxis("Horizontal") < -0.1f)
{
transform.localScale = new Vector3(-0.5f, 0.5f, 0.5f);
Right();
}
if (Input.GetAxis("Horizontal") > 0.1f)
{
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Left();
}
if (Input.GetButtonDown("Jump")) {
if (grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
canDoubleJump = true;
}
else
{
if (canDoubleJump)
{
canDoubleJump = false;
rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
rb2d.AddForce(Vector2.up * jumpPower / 1.75f);
}
}
}
if (curHealth > max_health)
{
curHealth = max_health;
}
if (curHealth <= 0)
{
curHealth = 0;
Die();
}
}
void FixedUpdate()
{
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
float h = Input.GetAxis("Horizontal");
/*//Fake friction / Easing the x speed of our player
if (grounded)
{
rb2d.velocity = easeVelocity;
}
*/
//Moving the player
rb2d.AddForce((Vector2.right * speed) * h);
//Limiting the speed of the player
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
public void Damage(int pain)
{
curHealth -= pain;
}
public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir)
{
float timer = 0;
while (knockDur > timer)
{
timer += Time.deltaTime;
rb2d.AddForce(new Vector3(knockbackDir.x * -100, knockbackDir.y * knockbackPwr, transform.position.z));
}
yield return 0;
}
void Right()
{
fp.transform.localRotation = Quaternion.Euler(0, 0, 180f);
}
void Left()
{
fp.transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
This is my missile script:
using UnityEngine;
using System.Collections;
public class Homing : MonoBehaviour
{
private Player player;
public float speed = 5;
public float rotatingSpeed = 200;
public int pain = 10;
public GameObject target;
Rigidbody2D rb;
// Use this for initialization
void Start()
{
target = GameObject.FindGameObjectWithTag("Play");
player = GameObject.FindGameObjectWithTag("Play").GetComponent<Player>();
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 point2Target = (Vector2)transform.position - (Vector2)target.transform.position;
point2Target.Normalize();
float value = Vector3.Cross(point2Target, transform.right).z;
/*
if (value > 0) {
rb.angularVelocity = rotatingSpeed;
} else if (value < 0)
rb.angularVelocity = -rotatingSpeed;
else
rotatingSpeed = 0;
*/
rb.angularVelocity = rotatingSpeed * value;
rb.velocity = transform.right * speed;
}
public void OnTriggerEnter2D(Collider2D hitInfo)
{
if (hitInfo.tag == "Play")
{
player.Damage(pain);
}
Destroy(gameObject);
}
}
Could anyone help me? Thanks.