- Home /
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.
I was thinking should I instantiate a gameobject, ridgebody, or transform? Thanks.
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.
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.
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.
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;
}
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.
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; }
Your answer
Follow this Question
Related Questions
Scripting errors only when building for iPhone 2 Answers
Can't seem to destroy instantiated objects. 1 Answer
Network Instantiate with GUI controls - iphone 0 Answers
Spawning hundred enemies on iPhone 4 Answers
Fruit Ninja - Cut fruit effect 3 Answers