- Home /
How to move an object to a certain distance from another object?
Hi, I am trying to make my script teleport/set the location of any enemy to a certain distance from the character but I have tried numerous amounts of attempts at doing this and I have failed every time so I decided to ask here how to do it. Excuse my noobiness, I am I really new to all of this.
I want to move my enemy in front of my character but a set amount of distance away.
Thanks for the help (:
var myTimer : float = 30;
var number = 0;
function Start ()
{
number = Random.Range(1, 256);
}
function Update () {
var Char = GameObject.Find("FPC");
var Jeff = GameObject.Find("Jeff");
Screen.showCursor = false;
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if (myTimer <=0 && number > 1){
myTimer = 120.0;
}
else if (myTimer <=0 && number == 1){
//move enemy here
myTimer = 30;
}
if (Input.GetButton ("Quit")){
Application.LoadLevel("Menu");
}
}
Answer by VicM · Dec 22, 2012 at 05:43 AM
mmm, I will try to help, apologies if I don´t understand you question in full.
In your code I do not see any translation or modification of the enemy transform so that is why I suppose you don´t see any movement. I only see timer stuff.
Basically you will want to do something like this (pseudocode C# style)
float delta1 = 50; // amount to displace the enemy from the character on x
float delta2 = 70; // amount to displace the enemy from the character on z
Vector3 currentCharacterPos = mycharacter.transform.position;// store character position
/ reposition the enemy at some position far away from my character using the character actual position/
enemyGameObject.transform.position = Vector3 (currentCharacterPos.x + delta2, currentCharacterPos.y, currentCharacterPos.z + delta1);
Hope it helps!
Also check the doc regarding transform, take a look at position and translate http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
Okay I fixed up my script as you said but i'm receiving this whenever the timer runs down:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Timer.Update () (at Assets/Timer.js:30)
Hi,
ok, that errors do not help too much as we cannot see your code.
I will try to guess. $$anonymous$$y hunch is that on the game object where you attached your script you are not assigning a gameobject to the variable you set as public inside the inspector, or inside your code whether you are not initializing a variable you are using or the object you are trying to attach to a variable does not exists.
$$anonymous$$y advice also would be to read your error messages carefully, double click them. $$anonymous$$ost of the time the error messages are self explanatory and give us enough context to fix them.
Post the code and explain which lines are the involved in the problem.
Also check if the game objects "FPC" and "Jeff" indeed exists. Check the names (spelling). You can create a verification to debug the problem.
if (GameObject.Find("Jeff") != null && GameObject.Find("FPC") != null) { // proceed } else { Debug.LogError("object(s) were not found!"); return; }
var myTimer : float = 1; var number = 0; var Char : GameObject; var Jeff : GameObject;
function Start () { number = Random.Range(1, 1); }
function Update () {
Char = this.gameObject; Jeff = GameObject.Find("Jeff");
var mousePos = Input.mousePosition; mousePos.x -= Screen.width/2; mousePos.y -= Screen.height/2;
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if (myTimer <=0 && number > 1){
myTimer = 120.0;
}
else if (myTimer <=0 && number == 1){
Jeff.transform.position = Vector3 (Char.x, Char.y, Char.z + 256);
myTimer = 30;
}
if (Input.GetButton ("Quit")){
Application.LoadLevel("$$anonymous$$enu");
}
}
This line of code:
Jeff.transform.position = Vector3 (Char.x, Char.y, Char.z + 256);
Has this error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Timer.Update () (at Assets/Timer.js:33)
Answer by emingo · Dec 22, 2012 at 11:13 PM
After seen your code I think the problem is that Char is a GameObject, it needs to be a Vector3, the transform.position of Char is what you want, but since the gameObject is targeting itself you don't even need that Char variable just do
Jeff.transform.position = Vector3 (transform.position.x, transform.position.y, transform.position.z + 256);
ps: 256 is big distance just in case.
Try this as stated above, just to clarify your $$anonymous$$d. It is the same but using your initial logic.
Jeff.transform.position = Vector3 (Char.transform.position.x, Char.transform.position.y, Char.transform.position.z + 256);
Basically Char.transform.position is equal to transform.position as you are targeting the same game object.
What you want is the transform on your Character, as it is a Game Object you need first to access its transform.
Thank you guys so much for the great help, my script is running perfectly fine now!
Cheers!