- Home /
Trouble With Positioning Gameobject : Unity3d
Hello everyone ,
I am making 2d game and in that game scene i am translating zombie game object from right to left.
Zombie is having box collider and rigidbody component.
Have a look at my scene :
Now when they come to the fence means collides with the fence , i want them to position in proper manner as they are not at proper position as you can see in image.
So i have written code to position them in nearest empty position when they collides with fence. But bcz of the rigidbody zombies are not collides with fence hence they dont find empty position.
COde which i have written for zombie is here :
public var maxSpeed : float ;
public var minSpeed : float ;
private var x : float;
private var y : float;
private var z : float;
private var prev : float = 0.0;
private var next : float;
private var currentSpeed : float;
private var stop : boolean = false;
private static var fenceEnemy : int;
private var one : boolean = false;
private var nearestPosition : GameObject;
//for glow effect : material alpha color set 105 , 255 ,255 , 255
function Start () {
SetSpeed();
}
function Update () {
var amttomove : float = currentSpeed * Time.deltaTime;
if(!stop)
transform.Translate(Vector3.left * amttomove , Space.World);
}
function SetSpeed()
{
currentSpeed = Random.Range(minSpeed , maxSpeed);
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag == "Fence")
{
stop = true;
gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
fenceEnemy++;
nearestPosition = GetNearestTaggedObject();
gameObject.transform.position = nearestPosition.transform.position;
nearestPosition.gameObject.tag = "full";
//gameObject.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
//yield WaitForSeconds(3.00f);
//Destroy(gameObject);
}
if(collision.gameObject.tag == "human")
{
Physics.IgnoreCollision(collision.gameObject.collider, collider);
}
if(collision.gameObject.tag == "zombie")
{
Physics.IgnoreCollision(collision.gameObject.collider, collider);
}
}
function GetNearestTaggedObject() : GameObject {
// and finally the actual process for finding the nearest object:
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag("empty");
var nearestObj : GameObject = null;
// loop through each tagged object, remembering nearest one found
for (var obj : GameObject in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj;
nearestDistanceSqr = distanceSqr;
}
}
return nearestObj;
}
So hope you understand the whole situation. if you dnt understand then plz ask me again..
I need your help guys.. Thanks for helping me and supporting me
I have used this logic to position them but if you have any other idea and logic then it will be appreciated.. Thanks..
make empty game objects for every zombies in the fence as u want and translate the zombies towards it
But there will be so many number of zombie in perticular level, and i dont want my zombie to cross fence so i want to arrange zombie in proper manner near fence,.
And my zombies are co$$anonymous$$g from random position..
Here, bcz of the rigidbody of zombie, all zombie are not able to collides with fence, so they are not getting nearest empty position and which does not look gud. If any zombie find other zombie in his way, then they stop translating forward.
Hope you understand.
Answer by yogee · Oct 25, 2013 at 10:33 AM
if u r generating random zombies means, movie the first zombie towards target (x=100,Y and Z are same),and next one move towards(target+=2) like this for even zombie set target as target+=2 and for odd one target-=2, when colliding with fence stop the movement
Your answer
Follow this Question
Related Questions
How do i Clamp the Z position of a rigidbody? 1 Answer
A node in a childnode? 1 Answer
Freeze Position problem 1 Answer
Physics not working properly 1 Answer
How would I make an object disappear after a set amount of time? 3 Answers