Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 14, 2018 at 07:32 AM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by catruzz · Sep 13, 2018 at 08:35 AM · prefab changing at runtimemissilehoming

Moving a prefab clone also moves all other clones in the scene

Hello there, I'm working on a 3D combat racing game (C#) and I'm stuck with the first weapon: missile.
The homing script works good (thanks go to this guy): the player picks up the weapon crate, a missile is instantiated on the top of the car and then, by pressing the fire button, the missile is launched.
The problem is that all the missiles in the scene are launched together (e.g. enemies' ones). By now I'm using standard assets for the vehicles.

WeaponCrate.cs (attached to the weapon crate, that is a simple red cube): when the player picks up the crate, a missile clone is instantiated on the top of his car only if the weapon slot is empty.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponCrate : MonoBehaviour {
 
     // weapons
     public GameObject missile;
 
     void OnTriggerEnter(Collider other)
     {
         // (with standard assets, players and bots collide with Payer/Bot > Colliders > ColliderBottom,
         // so we need to look for parent of parent to get the Player/Bot object)
         if (
             (other.gameObject.tag == "Player" || other.gameObject.tag == "Bot") &&
             other.transform.parent.parent.GetComponent<Player>())
         {
             pickup(other.transform.parent.parent.gameObject);
         }
     }
 
     void pickup(GameObject player)
     {
         bool pickupComplete = false;
 
         // sets player.weaponSlotFull equal to true
         if (!player.GetComponent<Player>().weaponSlotFull)
         {
             player.GetComponent<Player>().weaponSlotFull = true;
             pickupComplete = true;
         }
 
         if (pickupComplete)
         {
             Vector3 weaponPosition = player.transform.position;
             // place weapon on the roof of the player
             weaponPosition.y += 2;
 
             // instantiates weapon
             GameObject missileClone =
                 Instantiate(missile, weaponPosition, player.transform.rotation) as GameObject;
 
             // disable collisions on the object before attaching it to the player
             if (missileClone.GetComponent<Collider>()) missileClone.GetComponent<Collider>().enabled = false;
             // don't allow physics to affect the object before attaching it to the player
             if (missileClone.GetComponent<Rigidbody>()) missileClone.GetComponent<Rigidbody>().isKinematic = true;
 
             // as a child of player
             missileClone.transform.parent = player.transform;
 
             // removes object from the scene
             Destroy(gameObject);
         }
     }
 }


WeaponMissile.cs (attached to the missile prefab using the Unity interface): when the fire button is pressed, the missile is launched pointing the nearest GameObject tagged "Bot".

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponMissile : MonoBehaviour {
 
     public float speed = 40.0f;
     public float torque = 2.0f;
     private Rigidbody rigidBody;
     public Transform target;
 
     // Use this for initialization
     void Start () {
                 rigidBody = transform.GetComponent<Rigidbody>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetButtonDown("Fire1"))
         {
             Fire();
         }
 
     }
 
     void FixedUpdate()
     {
         if (target == null || rigidBody == null) return;
 
         rigidBody.velocity = transform.forward * speed;
 
         var targetRotation = Quaternion.LookRotation(target.position - transform.position);
         rigidBody.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, torque));
     }
 
     void Fire()
     {
         // enable collisions on the missile (they were disabled when attaching to the player)
         if (gameObject.GetComponent<Collider>()) gameObject.GetComponent<Collider>().enabled = true;
         // allow physics to affect the missile (they were disabled when attaching to the player)
         if (gameObject.GetComponent<Rigidbody>()) gameObject.GetComponent<Rigidbody>().isKinematic = false;
 
         float distance = Mathf.Infinity;
 
         foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Bot"))
         {
             Vector3 diff = enemy.transform.position - transform.position;
             float actualDistance = diff.sqrMagnitude;
 
             if (actualDistance < distance)
             {
                 distance = actualDistance;
                 target = enemy.transform;
 
                 // sets player.weaponSlotFull equal to false
                 // (weapon is a child of the player, so we need to look for parent to get the Player/Bot object)
                 if (gameObject.transform.parent)
                 {
                     GameObject player = gameObject.transform.parent.gameObject;
                     if (player.GetComponent<Player>() && player.GetComponent<Player>().weaponSlotFull)
                     {
                         player.GetComponent<Player>().weaponSlotFull = false;
                     }
                 }
             }
         }
     }
 }


I've already looked to the forums but all the relevant questions have been solved by removing some static variables, which I do not use at all.

Thank you

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by tormentoarmagedoom · Sep 13, 2018 at 08:45 AM

Good day.

The problem is simple, all your missiles have this code:

      void Update () {
      if (Input.GetButtonDown("Fire1"))
      {
          Fire();
      }

So when the button is pressed, all the missil scripts execute Fire().

IF the game is singleplayer, the solution is simple, make the if with 2 conditions (something like this)

    void Update () {
      if (Input.GetButtonDown("Fire1") && (gameObject.transform.parent == player))
      {
          Fire();
      }

So only the player missile will be shot.

If its s multiplayer, you will need to do detect if the missile is in the client player object.

Bye!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image catruzz · Sep 13, 2018 at 10:15 AM 0
Share

So basically I have to create a reference to the player the missile is attached to.
$$anonymous$$y god, couldn't be simpler!
Thank you man, you pointed me in the right direction.

avatar image tormentoarmagedoom catruzz · Sep 14, 2018 at 07:29 AM 0
Share

Thats why we are here :D

Good luck!

PD : I close the issue!

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Homing missile and LookAt issue 1 Answer

Homing missile not working. 1 Answer

Homing Missiles that target where you were 0 Answers

Creating a homing missile on 2D, problem with transform.lookat() 1 Answer

Dodgeable homing missiles in Unity3D 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges