- Home /
Keep minimum distance while moving.
Hi Guys, Hope you are doing well. I have struck in a problem, I have googled it but did not found any satisfactory answer. My objective is to move different gameobjects randomly, like cats are moving in a room but want to keep a minimum distance among them.
I have tried the following code but the gameobject jerks while playing.
#pragma strict
var Kitten_RandomPosition : Vector3;
var HouseCat_RandomPosition : Vector3;
public var Kitten : GameObject;
public var HouseCat : GameObject;
InvokeRepeating("ChangeKittenPosition", 0, 1);
InvokeRepeating("ChangeHouseCatPosition", 0, 1.4);
function Start ()
{
Kitten_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
HouseCat_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
}
function Update ()
{
if((Vector3.Distance(Kitten.transform.position, HouseCat.transform.position))<=2)
{
ChangeKittenPosition();
}
Kitten.transform.position = Vector3.MoveTowards(Kitten.transform.position, Kitten_RandomPosition, 2*Time.deltaTime) ;
PlumpCat.transform.position = Vector3.MoveTowards(PlumpCat.transform.position, PlumpCat_RandomPosition, 2*Time.deltaTime) ;
}
function ChangeKittenPosition()
{
Kitten_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
}
function ChangeHouseCatPosition()
{
HouseCat_RandomPosition = Vector3(Random.Range(-8.0, 8.0),Random.Range(-4.0, 4.0), 0);
}
Any Idea would be appreciated. Thank you
not quite. highlight ALL of your code in the original question and click on the 101/010 button at the top of the edit box. without proper formatting your code is hard to read and unlikely to get anyone to help.
please delete your comment once you've fixed the original question.
You should use an array of cats to avoid having to assign a variable for each cat.
public Cat[] Cats;
I don't know what the JS equivalent is, since I don't personally like the syntax so I never bothered. You can use a dictionary if you want to have named cats. Again, don't know what the JS equivalent is.
I have done this, thanks for the advice, but the issue remains i.e. "How to keep a $$anonymous$$. distance b/w them while moving".