• 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 Bendezium · Nov 27, 2011 at 04:55 PM · transformmoveinstancelocalinstatiate

How to move an instantiated object a fixed distance in local space?

I know it seems so simple, but I've been working on this for several hours now and I feel like I have tried everything I can find on unity3d.com, google and youtube. It is for a simple whack-a-mole game.

I at least have everything moving to a fixed location, but it is using world space and I haven't figured out a way to have it occur over 1 second of real time. Could someone point me in the right direction? I'd like to have each instance of this prefab move up 1 unit when it is instantiated, wait several seconds, and then move down. This is the script I have attached to my prefab:

 static    var moveSpeed:float = 10.0;
 
 function Update () {

 var bottomPosition:Transform;
 var topPosition = Vector3(0,1,0);

     moleAlive = true;
     gameObject.transform.position = Vector3.Lerp(transform.position, topPosition, (Time.deltaTime * moveSpeed));

 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Nov 28, 2011 at 01:40 AM

You can use a coroutine such as the ones in MoveObject. In this case you can do:

 var moveSpeed = 10.0;
 var waitTime = 3.0;
 
 function Start () {
     yield MoveObject.use.Translation (transform, Vector3.up, moveSpeed, MoveType.Speed);
     yield WaitForSeconds (waitTime);
     MoveObject.use.Translation (transform, -Vector3.up, moveSpeed, MoveType.Speed);    
 }

Note that you don't want to use static variables for this sort of thing. Static means that only one instance of that variable can exist, and you can't edit them in the inspector.

Comment
Add comment · Show 5 · 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 Bendezium · Nov 28, 2011 at 08:18 PM 0
Share

Thanks for your attention Eric5h5. It looks like I was on the right track because I did try this but it wasn't working (and still isn't). I've been working on this all morning/afternoon and still can't get it working.

I'm getting the following error when the game runs: NullReferenceException: Object reference not set to an instance of an object It is referencing this line in my script:


yield move$$anonymous$$ole.use.Translation (transform, Vector3.up, moveSpeed, Time.deltaTime);

This is the most recent code out of what seems to be a million different things I've tried the past couple hours. It is attached to the prefab normal$$anonymous$$ole:

var moveSpeed:float = 10.0; var waitTime:float = 3.0; var moleAlive = false; var move$$anonymous$$ole:GameObject; move$$anonymous$$ole = GameObject.Find("instNormal$$anonymous$$ole");

function Start () { moleAlive = true; yield move$$anonymous$$ole.use.Translation (transform, Vector3.up, moveSpeed, Time.deltaTime); yield WaitForSeconds (waitTime); move$$anonymous$$ole.use.Translation (transform, -Vector3.up, moveSpeed, Time.deltaTime);
moleAlive = false; }

And this is the code I have attached to an empty GameObject which instantiates the prefab normal$$anonymous$$ole every few seconds:

var $$anonymous$$oleTransformArray = new GameObject[5]; var mole:GameObject; var instNormal$$anonymous$$ole:GameObject;

//Timer Variables var spawnTimer:float = 0.0; var roundTime:float = 30.0; var roundBreak:float = 5.0; var moleLife:float = 3.0;

//$$anonymous$$ovement Variables var moveSpeed:float = 1.0; var fUpTime:float = 10.0; var moveDistance:float = 1.0;

//$$anonymous$$ole State Variables var b$$anonymous$$oleAlive:boolean = false;

function Update () {

 var spawnTransform:GameObject;
 var random$$anonymous$$ole:GameObject;
 spawnTimer += Time.deltaTime;

 //Spawn the mole
 if (roundBreak >= 5.0){
     if (spawnTimer >= 3.0){

         //Select the null and set position
         random$$anonymous$$ole = $$anonymous$$oleTransformArray[Random.Range(0, 4)];
         instNormal$$anonymous$$ole = Instantiate(mole, random$$anonymous$$ole.transform.position , Quaternion.identity);

         //Print information and reset timer
         print(random$$anonymous$$ole.name);
         print(spawnTimer);
         spawnTimer = 0.0;
     }
 }           

}

At this point I'm stumped. I've tried so many different things and I know I'm sooooo close.

avatar image Eric5h5 · Nov 28, 2011 at 08:56 PM 0
Share

@Bendezium: Please read the instructions on the $$anonymous$$oveObject page and follow them; you're not referring to the $$anonymous$$oveObject script at all:

Put this script in your Standard Assets/Scripts folder; this way it can be easily used from C# or Boo. The script should be named "$$anonymous$$oveObject". The script must be attached to some object in the scene, such as an empty object used for game manager scripts. You then use the coroutines by calling $$anonymous$$oveObject.use.Translation or $$anonymous$$oveObject.use.Rotation.

You really don't want to use Update for things like this; it's confusing and not efficient. Use $$anonymous$$oveObject or something else like iTween.

avatar image Bendezium · Nov 28, 2011 at 09:06 PM 0
Share

Lol! Sorry I misunderstood. I tried your code originally and Unity was encountering errors because $$anonymous$$oveObject wasn't defined. I thought you just used "$$anonymous$$oveObject" as a placeholder name because when I search unity's script reference for "$$anonymous$$oveObject", nothing turns up: No results found

I think I just found the page you think I should reference. Is this it: $$anonymous$$oveObject reference? If not can you please provide a link for me?. Thanks!

avatar image Eric5h5 · Nov 28, 2011 at 09:55 PM 0
Share

@Bendezium: Please don't post comments as answers. I linked to the $$anonymous$$oveObject page in the first line of my answer.

avatar image Bendezium · Nov 30, 2011 at 02:28 AM 0
Share

Thanks. I got it working very easily using the link in your original response. Sorry for my noobism.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can you help me understand this Camera calibration between Kinect and PS Move? 2 Answers

Help with local and world translation.position 0 Answers

Instantiate VS Transform 1 Answer

Transform global quaternion from local quaternion 2 Answers

Keep a min distance away from object while moving towards it 0 Answers


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