- Home /
How do I make a game object freeze in the y axis when hitting a collider?
#pragma strict
var Ggroup : GameObject[];
var group: GameObject[];
var moveSpeed : float = 0;
var speed : float = 6;
function Start ()
{
Physics.IgnoreLayerCollision(8,9,true);
Ggroup = GameObject.FindGameObjectsWithTag("Enemy1");
for(var i=0;i<6;i++)
{
for each (E in Ggroup)
{
if (Vector3.Distance(E.transform.position,transform.position)<150)
{
group[i]=E;
break;
}
}
}
}
function Update ()
{
for (var x=0;x<group.Length;x++)
{
if(Vector3.Distance(group[x].transform.position,transform.position)<3)
{
Debug.Log("avoid");
var delta = group[x].transform.position - transform.position;
delta.Normalize();
delta = Vector3.one-delta;
transform.position = transform.position + (delta * moveSpeed);
}
}
}
This is the script I gave to enemies that spawn so that they avoid each other (part of a flocking algorithm), the problem is that sometimes to avoid each other they jump in the y axis rather than moving away in the horizontal axes of the scene. I think the best way to avoid this would be to freeze them in the y axis once they hit the collider of the terrain (they spawn higher up and I don't want them to freeze in the y axis on spawn), is it possible for me to do it? What would I need to do?
Answer by Hyperion · Dec 21, 2013 at 07:57 PM
If you wish to permanently freeze them in the Y-axis, click their rigidbody, click constraints, and tick whatever option/axis you want for freezing.
Your answer
Follow this Question
Related Questions
Need help with game points and trigger 2 Answers
Ignore Collision problem 1 Answer
Why can't I check to see if a Collider component is null? 2 Answers
problem with terrain collider 0 Answers
Object Carry 0 Answers