- Home /
The question is answered, right answer was accepted
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
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!
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.
Thats why we are here :D
Good luck!
PD : I close the issue!
Follow this Question
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