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
1
Question by TomMakesGames · Sep 10, 2013 at 02:34 PM · rotationrotate object

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.

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 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.)

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 TomMakesGames · Sep 10, 2013 at 04:09 PM 1
Share

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).

avatar image Fattie · Sep 10, 2013 at 06:09 PM 0
Share

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

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

16 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 avatar image avatar image avatar image avatar image avatar image

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


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