- Home /
PreFabs not colliding with other GameObjecs, but collide with themselves.
i'm fairly new to unity and currently in process making my first game using unity.
i have currently a player(GameObject: sphere) revolving around a centre point in circular motion.
Another enemy, it is a prefab and i instantiate after a random amount of time and move it towards the centre around which the player revolves.
code for the instantiation of the PreFab enemy.
using UnityEngine;
using System.Collections;
public class enemy_movement : MonoBehaviour {
public float targetTime;
int i=0;
int num=-1;
int temp;
public Transform[] patrolpoints;
public Transform planetposition;
public int movespeed;
private int currentposition;
public int size;
GameObject[] enemy=new GameObject[500];
GameObject PreFab;
// public GameObject deathparticles;
// Use this for initialization
void Start () {
targetTime=random_value_float();
PreFab=(GameObject)Instantiate(Resources.Load("enemy"));
PreFab.name="enemy";
//currentposition=0;
}
// Update is called once per frame
void Update () {
targetTime -= Time.deltaTime;
if((targetTime<=0 && num<=1))
{
num=num+1;
temp=random_value();
//this is the error line
enemy[num] = (GameObject)Instantiate(PreFab, patrolpoints[temp].position, Quaternion.identity);
enemy[num].name="enemy";
targetTime=random_value_float();
}
for(i=0;i<=num;i++)
enemy[i].transform.position=Vector3.MoveTowards(enemy[i].transform.position, planetposition.position , movespeed*Time.deltaTime);
}
int random_value()
{
int num = Random.Range(0, patrolpoints.Length);
return num;
}
float random_value_float()
{
float num = Random.Range(5, 30) * .1f;
return num;
}
}
followed by the code of the player, that detects collision with the enemy prefab.
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour {
public Transform playerpos;
int flag=0;
float angle =0;
float speed=(4*Mathf.PI)/5 ;//2*PI in degress is 360, so you get 5 seconds to complete a circle
float radius=1.8f;
float x;
float y;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider collision)
{
Debug.Log ("Called by "+ transform.gameObject.name);
if(collision.gameObject.name == "enemy")
print("player hit " + collision.gameObject.name);
//else
//{
// Debug.Log("Hit something else!"+ collision.gameObject.tag);
// }
}
// Update is called once per frame
void Update()
{
if(flag==0)
{
angle += speed*Time.deltaTime; //if you want to switch direction, use -= instead of +=
//flag=1;
}
else if(flag==1)
{
angle -= speed*Time.deltaTime; //if you want to switch direction, use -= instead of +=
//flag=0;
}
if (Input.GetKeyDown("space"))
{
if(flag==0)
flag=1;
else if(flag==1)
flag=0;
}
x = Mathf.Cos(angle)*radius -1.06f ;
//x=x-1.12;
y = Mathf.Sin(angle)*radius -1.04f;
//y=y-0.56;
transform.position = new Vector3(x, y, 0);
// print("up arrow key is held down");
}
}
now the issue
the enemy prefabs (that are instantaited) can collide with each other but not with the player.
if i place the enemy near the player (without instantaiting it) it gets collided with the player .
player is a sphere with isTrigger checked and Rigidbody component added,same goes for the enemy PreFab. and have tried both names and tags , nothing works.
any advice would be appreciated.
thanks!!
Check the Physics settings, and make sure both layers can register collisions in the Collision $$anonymous$$atrix.
how to check the physics settings and both of the game objects are in the same layer...the default layer.
Google is your friend: Physics $$anonymous$$anager
Try OnTriggerStay ins$$anonymous$$d of OnTriggerEnter to male sure the lack of collision registering is not due to the way you move your objects. Setting the transform.position of physics objects directly can lead to such errors.