Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by YeeLuke · Dec 21, 2012 at 06:50 PM · distancemovehow tofrom

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");
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YeeLuke · Dec 22, 2012 at 12:38 AM 0
Share

Still haven't found the resolution. :|

avatar image YeeLuke · Dec 22, 2012 at 05:09 AM 0
Share

Hmm :( Bump?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image YeeLuke · Dec 22, 2012 at 06:46 AM 0
Share

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)

avatar image VicM · Dec 22, 2012 at 02:38 PM 0
Share

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; }

avatar image YeeLuke · Dec 22, 2012 at 05:32 PM 0
Share

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");
 }

}

avatar image YeeLuke · Dec 22, 2012 at 05:35 PM 0
Share

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)

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VicM · Dec 23, 2012 at 04:08 AM 0
Share

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.

avatar image YeeLuke · Dec 23, 2012 at 03:44 PM 0
Share

Thank you guys so much for the great help, my script is running perfectly fine now!

Cheers!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

move FP controller on trigger enter? 0 Answers

Can't access variable from another script. 1 Answer

Touch and move in any axis 0 Answers

Very Simple Left and Right Movement Script [OR] disable dravity on CharacterController? 2 Answers

Get vector3 from another script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges