Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by niter412 · Dec 14, 2009 at 07:43 PM · instantiateiphonecs1612

Moving Object after spawn

Hi,

After an object is instantiated, I would like it to move straight line across the map back and forth.

So I was wondering what would be most efficient way to do that? Thank you very much.

Comment
Add comment · Show 1
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 niter412 · Dec 14, 2009 at 07:53 PM 0
Share

I was thinking should I instantiate a gameobject, ridgebody, or transform? Thanks.

4 Replies

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

Answer by duck · Dec 14, 2009 at 09:03 PM

Make a script whose purpose is to move the object across the map back and forth. For example (manipulating position, not physics):

function Update ()
{
    transform.position.x = Mathf.PingPong(Time.time, 20);
}

Place this script on the prefab object that you're instantiating. Then, when you instantiate the prefab, you'll also get an instance of this script, which will begin execution immediately. For the purposes of this example, the script will move the object back and forth across the area from 0-20 units, world position. If you have more specific requirements, please say, and I can refine this answer.

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 niter412 · Dec 15, 2009 at 03:23 AM 0
Share

Hi,

I tried the code above and I got an error: error CS1612: Cannot modify the return value of `UnityEngine.Transform.position' because it is not a variable

I am not sure what that means. Thanks.

avatar image duck ♦♦ · Dec 15, 2009 at 09:48 AM 0
Share

Sounds like you're converting it to C#, right? In that case you can't assign to the .x .y or .z of a transform's position directly. Ins$$anonymous$$d you have to assign a whole new Vector3 to the position in one go.

avatar image
0
Best Answer

Answer by rocket5tim · Dec 17, 2009 at 02:19 AM

Here's another way to move an object back and forth (or up and down). I don't know if this method is more or less efficient than the Mathf.PingPong example, but I find that this gives me finer control over where the object changes direction.

Add this script to a game object (like a sphere) and put it at 0,0,0. You should see your sphere move between 2.5 and -2.5 on the Y axis.

var speed : float = 2.0; private var trans : Transform; private var dir : int = 1;

function Awake () { trans = transform; }

function Update () { var moveSpeed = speed * Time.deltaTime; enemyPos = transform.localPosition.y;

 trans.Translate(0, moveSpeed*dir, 0);
 if(enemyPos >= 2.5) // this is in world coordinates
     dir = -1;
 if(enemyPos <= -2.5) // this is in world coordinates
     dir = 1;

}

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

Answer by Ehren · Dec 14, 2009 at 09:14 PM

You can also use the Animation Editor to create an animation that starts automatically once the object is instantiated and changes the GameObject's transform.position.x value over time. To get it to repeat, change the drop-down in the lower left-hand of the Animation Editor from "Default" to "Loop" or "Ping Pong", depending on the effect you're going for.

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

Answer by Kyle 1.du · Apr 01, 2010 at 02:21 PM

If you'd like a smoother animation for back and forth you could do the following

var degrees : float = 0; var maxValue : int = 10;

function Update(){

transform.Translate(0, maxValue Mathf.Sin(degrees (Mathf.PI/180)), 0);

//keeps degrees from getting too high degrees > 360 ? degrees -= 360 : degrees += 2; }

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

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

No one has followed this question yet.

Related Questions

Network Instantiate with GUI controls - iphone 0 Answers

Can't seem to destroy instantiated objects. 1 Answer

Spawning hundred enemies on iPhone 4 Answers

Fruit Ninja - Cut fruit effect 3 Answers

Accessing prefab instances at runtime (unity iphone) 2 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