- Home /
Unity2D Parent of prefab is instantiating, but not moving.
Hi. I want to make some spikes as obstacles in my game. I wanted to give feedback based on where the bullet hits the spike. Since spike has a slanted surface, i couldnt use box colliders to check for triggers. So I split the spike into 4 objects, from top to bottom and added box colliders to each object. And then added them into a parent object called Spike. I made prefabs for all the 4 spike objects and the parent Spike object too. My problem is, I can instantiate the Parent Spike, but I couldnt move it. I added a rigidbody2D to it and still cant move it. In the Inspector window, the transform position value is changing, but nothing is changing in the scene/game views.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpikeManager : MonoBehaviour
{
public Transform spikeTopPoint;
public Transform spikeBottomPoint;
public GameObject spikePrefab;
private bool canMakeSpike = false;
public float spikeDelay = 1f;
public float spikeSpeed = 30f;
void Start()
{
canMakeSpike = true;
}
void Update()
{
if (canMakeSpike)
{
StartCoroutine(Spike());
}
}
public IEnumerator Spike()
{
//Instantiate your projectile
MakeSpike();
canMakeSpike = false;
//wait for some time
yield return new WaitForSeconds(spikeDelay);
canMakeSpike = true;
}
void MakeSpike()
{
//Transform spikePoint = spikeBottomPoint.transform;
GameObject spikeInstance = Instantiate(spikePrefab, spikeBottomPoint.position, spikeBottomPoint.rotation);
//spikeInstance.GetComponent<Rigidbody2D>().AddForce(-transform.right * spikeSpeed, ForceMode2D.Impulse);
//spikeInstance.GetComponent<Rigidbody2D>().velocity = Vector2.left * spikeSpeed * Time.deltaTime;
//spikeInstance.transform.Translate(-transform.right * spikeSpeed * Time.deltaTime);
//spikeInstance.transform.position -= transform.right * spikeSpeed * Time.deltaTime;
}
}
This is the code I'm using to instantiate the spike and to move it. As you can see, I tried different ways. The individual child spike parts are working fine. They give collider feedback and even move if i substitute them instead of the parent Spike.
If this is not solved, instead of box collider, I might try an edge collider and edit the edge to the slanted spike shape. But I wonder if I can check for the bounds of it.