Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Unity - Destroying game object with tag "enemy" with projectiles, ned help.

Options
  • 06-04-2016 7:21pm
    #1
    Registered Users Posts: 101 ✭✭


    Hey, so I feel that it's not a good sign I need a lot of help with this project XD Basically, I am making a 3rd person shooter, where the player is a robot who fights other robots by shooting them. However, the shots I fire do not destroy the enemies (labelled by tag "Enemy") but also, when they run into my robot, they then get destroyed. I was wondering how I can get it so the enemies get destroyed by my shot but don't get destroyed when they run into me. Here is my script:
    using UnityEngine;
    using UnityEngine.UI;

    public class TankShootingV2 : MonoBehaviour
    {
    public int m_PlayerNumber = 1; // Used to identify the different players.
    public Rigidbody m_Shell; // Prefab of the shell.
    public Transform m_FireTransform; // A child of the robot where the shells are spawned.
    public AudioSource m_ShootingAudio; // Reference to the audio source used to play the shooting audio. NB: different to the movement audio source.
    public AudioClip m_ChargingClip; // Audio that plays when each shot is charging up.
    public AudioClip m_FireClip; // Audio that plays when each shot is fired.
    public float m_MinLaunchForce = 15f; // The force given to the shell if the fire button is not held.
    public float m_MaxLaunchForce = 30f; // The force given to the shell if the fire button is held for the max charge time.
    public float m_MaxChargeTime = 0.75f; // How long the shell can charge for before it is fired at max force.

    private string m_FireButton; // The input axis that is used for launching shells.
    private float m_CurrentLaunchForce; // The force that will be given to the shell when the fire button is released.
    private float m_ChargeSpeed; // How fast the launch force increases, based on the max charge time.
    private bool m_Fired; // Whether or not the shell has been launched with this button press.





    private void Start()
    {
    // The fire axis is based on the player number.
    m_FireButton = "Fire" + m_PlayerNumber;

    // The rate that the launch force charges up is the range of possible forces by the max charge time.
    m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
    }


    private void Update()
    {
    // The slider should have a default value of the minimum launch force.


    // If the max force has been exceeded and the shell hasn't yet been launched...
    if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
    {
    // ... use the max force and launch the shell.
    m_CurrentLaunchForce = m_MaxLaunchForce;
    Fire();
    }
    // Otherwise, if the fire button has just started being pressed...
    else if (Input.GetButtonDown(m_FireButton))
    {
    // ... reset the fired flag and reset the launch force.
    m_Fired = false;
    m_CurrentLaunchForce = m_MinLaunchForce;

    // Change the clip to the charging clip and start it playing.
    m_ShootingAudio.clip = m_ChargingClip;
    m_ShootingAudio.Play();
    }
    // Otherwise, if the fire button is being held and the shell hasn't been launched yet...
    else if (Input.GetButton(m_FireButton) && !m_Fired)
    {
    // Increment the launch force and update the slider.
    m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
    }
    // Otherwise, if the fire button is released and the shell hasn't been launched yet...
    else if (Input.GetButtonUp(m_FireButton) && !m_Fired)
    {
    // ... launch the shell.
    Fire();
    }
    }


    private void Fire()
    {
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;

    // Create an instance of the shell and store a reference to it's rigidbody.
    Rigidbody shellInstance =
    Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;

    // Set the shell's velocity to the launch force in the fire position's forward direction.
    shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward; ;

    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play();

    // Reset the launch force. This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;
    }

    private void OnCollisionEnter(Collision collision)
    {
    // You probably want a check here to make sure you're hitting a zombie
    // Note that this is not the best method for doing so.
    if (collision.gameObject.tag == "Enemy")
    {
    Destroy(collision.gameObject);
    }
    }
    }

    I apologise for asking such a stupid question, I am just really stuck on this.


Comments

  • Registered Users Posts: 175 ✭✭Withrax


    OnCollisionEnter() gets collisions with the object the script is attached to (in this case your robot). So what you need to do is make a new script and attach it to the bullet prefab instead. Then have an OnColiisionEnter() bit in that like the one you have written. I you have any more questions ask away


Advertisement