- Home /
I need to instantiate a empty gameObject 2 Units right/left of a 'Enemies' Transform?
Hello, I need to instantiate a empty gameObject/Waypoint 2 Units right/left of a 'Enemies' Transform? I am making the enemies AI and need to drop a Waypoint either right or left of the enemy so if the enemy collids with a say a rock the enemy looks at the newly created Waypoint and goes to that Waypoint and then the Waypoint destories itself when triggered. The enemy then looks at the main object that it was trying to get to get to like in RTS Games... I hope that makes sense :P
Answer by Triqy · Apr 17, 2013 at 06:11 PM
i figured it out myself. This is what I used!
var CreateWaypoint = false;
function Upadate(){
if(CreateWaypoint == true){
var CorrectionWay = instantiate(CorrectionWaypoint, Vector3(transform.localPosition * 2.3f, transform.localPosition.y, transform.localPosition.z), transform.rotation);
CorrectionWay.name = "WayPoint";
CreateWaypoint = false;
}
}
This creates a waypoint about 2 units to the left of the enemy
Answer by 1337GameDev · Apr 14, 2013 at 09:35 PM
Well, to get the position left or right, just find the point left or right of where your enemy is. Here is some untested, top of my head, code ( c# ):
Transform enemyTransform; //set this to the enemy, however you want
Transform newTransform = enemyTransform;
float amount = 2.0f; //amount to check
newTransform.Translate(Vector3.right * amount); //move right
Instantiate(wayPointPrefab, newTransform.position, Quaternion.zero);
//this assumes a prefab has been loaded into wayPointPrefab, use Resources.Load for this or inspector variable
//Quaternion.zero rotation orients to world zero rotation, if you need something different use that, you could also use enemyTransform.rotation
I believe this will work fine. There might be a quicker way to do this with directly modifying x,y, or z components of newTransform.position, but this was the first way I thought of.
Remember this is untested, so there might be errors, but this should show the general methodology
Well I was planning on having the enemy turn towards the Waypoint and then when the enemy hits the Way turn back to the original destination using transform.LookAt(); (JS)
I can read C# but read/write in JS
The Quaternion.Zero? Will that trun the enemy toward the newly created Way or is that fpr the instainated prefab? Thanks
Quaterion.zero is just to give an all zero rotation to the instantiated prefab. You can change this to your desired direction to save some instructions.
Look at Aron greenberg's a star path finding project. It has a lot of path finding tools you could make use of. There is a free and paid version.
I would highly suggest learning c#, as its more similar to other program$$anonymous$$g languages and JS is unity specific. C# is also used outside of unity a lot, so you can find scripting help from non unity places. JS is also a sore to look at, atleast for me.
Answer by Owen-Reynolds · Apr 15, 2013 at 01:00 AM
When you say right or left, it usually means you're thinking in local coordinates (select a rotated object in the editor and toggle Local/Global at top-left to see the difference.) Your right, of the way you are facing now.
To move two units right (your right, local): Vector3 p2 = transform.position + transform.right*2;
. Trans.right is essentially the little Red direction arrow when you're moving an object.
To compare, world movement (east, in this case) is transform.position + Vector3.right*2;
If you're lying on your side, transform.right
could be up or down, but a typical "mob" is always head-up, so transform.L/R/F/B is always flat along the ground.
Yup. This seems correct at first glance.
Also, align your model so that the forward direction actually is forward. If you don't, his code will have your object calculate wrong movement (as right, left, forward and backward are relative to how the model is oriented).