Question by
Reedex · Nov 24, 2017 at 02:35 PM ·
scripting probleminstantiate prefab
turret shoot in direction and hit my health (scripts attached,i need to know how to put it together)
the thing is there are lot of tuts or vids, but since they are using diff codes it's kinda gibberish. so how do i make the turret shoot at me ?. basically ,instantiate a projectile while i am close is done and working ,so : 1) the projectile is not moving towards me though. + 2) once it hits how do i make ,to subtract health from other healthscript? thanks; :) codes:
1) - turret shots when sees me public class HJK : MonoBehaviour {
//[SerializeField] private float ahojda = 10f;
[SerializeField] private float rayLenght = 10f;
[SerializeField] private LayerMask newlayermask;
public GameObject projectile;
private GameObject raycastbiatch;
//[SerializeField] private
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Vector3 frwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(transform.position,frwd,out hit,rayLenght,newlayermask.value))
{
if (hit.collider.CompareTag ("Player"))
{
Instantiate (projectile, transform.position, transform.rotation);
}
}
}
}
2) turrets tracking sys
public class TrackingSystem : MonoBehaviour { public float speed = 3.0f; public Transform m_target = null; Vector3 m_lastKnownPosition = Vector3.zero; Quaternion m_lookAtRotation;
// Update is called once per frame
void Update () {
m_target = GameObject.FindGameObjectWithTag ("Player").transform;
if(m_target){
if(m_lastKnownPosition != m_target.transform.position){
m_lastKnownPosition = m_target.transform.position;
m_lookAtRotation = Quaternion.LookRotation(m_lastKnownPosition - transform.position);
}
if(transform.rotation != m_lookAtRotation){
transform.rotation = Quaternion.RotateTowards(transform.rotation, m_lookAtRotation, speed * Time.deltaTime);
}
}
}
bool SetTarget(Transform target){
m_target = GameObject.FindGameObjectWithTag ("Player").transform;
if(!target){
return false;
}
m_target = target;
return true;
}
}
3) my vitals
public Slider healthSlider;
public int maxHealth;
public float healthFallRate;
//
public Slider thirstSlider;
public int maxThirst;
public float thirstFallRate;
//
public Slider hungerSlider;
public int maxHunger;
public float hungerFallRate;
void Start(){
healthSlider.maxValue = maxHealth;
healthSlider.value = maxHealth;
thirstSlider.maxValue = maxThirst;
thirstSlider.value = maxThirst;
hungerSlider.maxValue = maxHunger;
hungerSlider.value = maxHunger;
}
void Update (){
//HP controler
if (hungerSlider.value <= 0 && (thirstSlider.value <= 0)) {
healthSlider.value -= Time.deltaTime / healthFallRate * 2;
} else if (hungerSlider.value <= 0 || thirstSlider.value <= 0) {
healthSlider.value -= Time.deltaTime / healthFallRate;
}
if (healthSlider.value <= 0) {
CharacterDeath ();
}
// Hunger Controller
if (hungerSlider.value >= 0) {
hungerSlider.value -= Time.deltaTime / hungerFallRate;
}
else if (hungerSlider.value <= 0) {
hungerSlider.value = 0;
}
else if (hungerSlider.value >= maxHunger)
{
hungerSlider.value = maxHunger;
}
// Thirst Controller
if (thirstSlider.value >= 0) {
thirstSlider.value -= Time.deltaTime / thirstFallRate;
}
else if (thirstSlider.value <= 0) {
thirstSlider.value = 0;
}
else if (thirstSlider.value >= maxThirst)
{
thirstSlider.value = maxThirst;
}
}
void CharacterDeath()
{
Debug.Log("died in engine,sheesh");
}
}
Comment