-1

I'm creating a game where objects are being created. These objects are moving left and the player has to press space bar before the object reaches the end of the screen. Every thing works fine with the exception of spawning a prefab on the moving objects.

The idea is to spawn a animation on the nearest target. But when instantiated, the prefab always instantiate at the prefab location (0,0,0). How do I make the instantiated prefab spawn at the correct place? (I've tested with print(nearestTarget.transform.position) and the print always gives me a different location depending on the "nearestTarget")

Trying to spawn the prefab in HandSlapAnimation() Method.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
    // Reference to levelManager
    [SerializeField] LevelManager levelManager;
    // Array of all present entities
    private GameObject[] targets;
    // Nearest entity
    private GameObject nearestTarget;
    // Array of all x position of all present entities
    private float[] locationOfTargets;
    // Nearest x position (transform.position.x) in all entities
    private float nearestLocation;
    // Finds the index location of the float value
    private int index;
    // Hand prefab to be instantiate during Keypress
    public GameObject handPrefab;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        targets = GameObject.FindGameObjectsWithTag("Entities");

        if (targets.Length > 0)
        {
            FindLocationOfEntities();
            AssignNearestTarget();
            HandSlapAnimation();
            DestroyNearestTarget();
        }
    }
}

void FindLocationOfEntities()
{
    locationOfTargets = new float[targets.Length];

    for (int i = 0; i < targets.Length; i++)
    {
        locationOfTargets[i] = targets[i].transform.position.x;
    }
}

void AssignNearestTarget()
{
    nearestLocation = Mathf.Min(locationOfTargets);
    index = System.Array.IndexOf(locationOfTargets, nearestLocation);
    nearestTarget = targets[index];
}

void DestroyNearestTarget()
{        
    if (nearestTarget.GetComponent<CubeMovement>().isCat == true)
    {
        levelManager.LoadLevel("Game Over");
    }
    else
    {
        Destroy(nearestTarget);
    }
}

void HandSlapAnimation()
{
    // Instantiate  prefab Hand GameObject at the nearest target to execute the Hand Slap animation
    Instantiate(handPrefab, transform.position, Quaternion.identity);
    handPrefab.transform.SetParent(nearestTarget.transform);
    print(nearestTarget.transform.position);
}

}

  • Well. No where in hand slap are you moving it. You have set its parent - that doesn’t move it - you instantiate it at where ever this code is running at. But you don’t set it’s position. Look at the parameters of instantiate or move it’s position – BugFinder Jun 02 '20 at 22:32

1 Answers1

0

The problem is that in:

Instantiate(handPrefab, transform.position, Quaternion.identity);

The transform.positionis the position of the actual object, your PlayerInput instance.

Then, you do: handPrefab.transform.SetParent(nearestTarget.transform) but that doesn't move the position, only sets the parent.

Instead of using transform.position in the Instantiate method, use nearestTarget.transform.position.

Instantiate(handPrefab, nearestTarget.transform.position, Quaternion.identity);
Marco Elizondo
  • 552
  • 3
  • 9