- Home /
Object spinning around it's own axis whilst travelling in a straight line
I've been using the following script to randomly rotate an object on instantiation and then have it travel in a straight line:
using System.Collections;
public class shrapnel : MonoBehaviour {
public float speed = 10;
Vector3 randomDirection;
Vector3 position;
// Use this for initialization
void Start () {
//Setting the angle and rotating the shrapnel, ready to move
float randomAngle = Random.Range (-35, 35);
transform.Rotate(0f, 0f, randomAngle);
}
// Update is called once per frame
void Update () {
//Shrapnel travels in direction of local rotation, in opposite direction to local up
transform.Translate((transform.up * -1) * speed * Time.deltaTime, Space.Self);
//Destroy when off the screen
if (transform.position.y < 0){
Destroy(this.gameObject);
}
if (transform.position.x < 0 || transform.position.x > Screen.width)
Destroy(this.gameObject);
//Apply spinning motion
}
}
It's a 2D game.
But since I'm using the local direction to move, if I use transform.Rotate to make the shrapnel spin on its axis whilst it goes in a straight line, it changes the trajectory of the object.
I've tried using rigidbody.AddTorque since I thought the torque axes would be separate to rotation axes, but it's having no effect (a rigidbody component HAS been attached).
Does anyone have any ideas what I could do here?
Many thanks in advance.
Answer by Fattie · Sep 10, 2013 at 02:40 PM
It's almost incredibly easy to do this - you'll be sick.
Say your object is called SHIP, ok.
Make a new object .. a wrapper object. So do this, make a new empty game object, called SHIPWRAPPER.
Put it in exactly the position of the ship. Now put the ship INSIDE the wrapper.
Now, just have SHIP itself spin, tumble or do anything you want.
Treat the actual WRAPPER object, as "the ship itself". So for example, say you have a script "make the ship travel to Canada" .. in fact you'd attach that to the WRAPPER, rather than the ship itself. And so on.
It's just that easy - the incredible beauty of the abstraction layer of "transforms" with quats in game engines. Hope it helps!
BTW the simplest way to make the SHIP spin (tumble, whatever) within the SHIPWRAPPER,
just use the ("traditional") animation system in unity. don't even bother trying to write a script.
(BTW for anyone reading who is brandnew to unity, take the opportunity to learn how to "spin" something using the animation screen ... it's super-easy - just click Apple-6, click record, click to add a curve on Z rotation and add a key point at zero seconds and a key point at 5 seconds, then set the two key points to 0 and 360 on the z rotation, and set to wrap down the bottom, that's it.)
That sounds great! I'll try that.
Unfortunately, I can't test it out quickly because I had lots of things checking the colour of the renderer of the original object, so if I put it in a wrapper I'm going to have to go and recode and bug fix a load of stuff. But the idea sounds totally legit.
$$anonymous$$any thanks!
BTW - I'm not sure if using the animation system in Unity is really a lot easier. $$anonymous$$aybe if you're an animator? I haven't even opened up the animator (kind of knew it was lurking there somewhere but I've never tried using it).
Hey @$$anonymous$$ !
"I had lots of things checking the colour of the renderer of the original object..."
It really shouldn't cause you too much trouble.
Say you have TEN things where there is an "editor link" via a variable. In those ten cases just drag the "new" spaceship there. BUT NOTE THAT moving around the spaceship, WON'T actually break those links.
Conversely if you use GameObject.Find() in your launch code - nothing to do at all. It's part of developing in Unity man! heh
Re the animator. Honestly it is super easy. You literally just do the six steps I mention.
click Apple-6,
click record,
click to add a curve on Z rotation (click the small "equals" looking icon on the right - it's dead simple)
and add a key point at zero seconds
and a key point at 5 seconds,
then set the two key points to 0 and 360 on the z rotation (ie click on the value field on the zrotation line),
and set to wrap down the bottom (popup),
that's it
Believe me I'm $$anonymous$$r "I hate learning curves" - its' very easy. Cheers
Your answer

Follow this Question
Related Questions
how to make a detect rotation script for negative rotation too 2 Answers
Rotate object respect other object rotation 0 Answers
How do I rotate an object when I press down a key and then it rotates back when I release the key 0 Answers
how to rotate a gameobject in a given direction and stop when it's done 1 Answer
Why one of object's animation has global rotation data, and another has local rotation data? 0 Answers