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
0
Question by TomMakesGames · Sep 09, 2013 at 01:03 PM · rotationdirectionlocalinstantiationworld

Mesh won't travel in direction it's facing

Hi,

I have an empty object instantiating 'shrapnel' gameobjects at a certain point in space.

This is a 2D game, so I only want objects to move in X and Y, not in Z.

When the pieces of shrapnel are instantiated, the gameObject has this script attached:

using UnityEngine; using System.Collections;

public class shrapnel : MonoBehaviour {

 float speed = 10;
 Vector3 randomDirection;
 
 
 // Use this for initialization
 void Start () {
     
     float randomAngle = Random.Range (-135, 45);
     randomDirection = new Vector3(0, 0, randomAngle);
     
     transform.Rotate(randomDirection);
     
     transform.forward = transform.rotation * new Vector3(1,1,1);
     
     
 
 }
 
 // Update is called once per frame
 void Update () {
     
     
     transform.Translate( transform.forward * speed * Time.deltaTime, Space.Self);
 
 }

}

Now the best that I've been able to do is to get the object to rotate around its Z-axis, but then it just keeps travelling 'into' the screen (i.e. away from the orthographic camera) rather than downwards (which is what I'm aiming for).

The above script has my 'bits of shrapnel' firing at all angles, even though I've only asked for them to rotate in one direction.

So, ultimately, what I'm looking to do is when this object spawns it rotates to an angle within a range (pointing down in an angle) and then moves in that direction down the screen.

What have I missed here?

Many thanks in advance.

Comment
Add comment · Show 3
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 ShadoX · Sep 09, 2013 at 03:11 PM 0
Share

Try replacing "Space.Self" with "Space.World"

 transform.Translate( transform.forward * speed * Time.deltaTime, Space.World);
avatar image TomMakesGames · Sep 10, 2013 at 10:03 AM 0
Share

That definitely helped - now the rocks are moving. But they're still moving in 3 dimensions, when they should only be moving in two. As far as I can see, I've only asked the prefab to rotate around one axis, meaning it should only travel along 2 axes. I've obviously missed something though.

avatar image ShadoX · Sep 10, 2013 at 12:07 PM 0
Share

Have you tried freezing the rotation or location?

1 Reply

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

Answer by $$anonymous$$ · Sep 09, 2013 at 04:49 PM

The following code should work.

 using UnityEngine; 
 using System.Collections;
 
 public class shrapnel : MonoBehaviour 
 {    
     float speed = 10;
     Vector3 randomDirection;
     
     // Use this for initialization
     void Start () 
     {        
         float randomAngle = Random.Range (-40, 40);    
         
         // Rotate object around z axis
         transform.Rotate(0f, 0f, randomAngle);                
     }
     
     void Update () 
     {    
         // (transform.up * -1) means objects down direction.
         transform.Translate( (transform.up * -1) * speed * Time.deltaTime, Space.Self);        
     }
 }

You could also look at AddExplosionForce for an easier way of doing this with Physics.

Comment
Add comment · Show 3 · 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 10:18 AM 0
Share

Awesome, just tried this and it works.

Only surprising thing is that the 'shrapnel' isn't orienting the way I thought it should be, but that doesn't really matter.

If I could trouble you again hdsnevi (because I want to understand what's going on exactly), if I'm telling the object to go down, how come it actually travels in the direction of the rotation?

Is it that down becomes the opposite of where the local 'up' is (seeing as I've specified Space.self)?

Again, thanks regardless.

avatar image Sajidfarooq · Sep 10, 2013 at 10:19 AM 0
Share

Please use a comment rather than an answer to reply to existing answers.

avatar image TomMakesGames · Sep 10, 2013 at 10:54 AM 0
Share

Sorry, I thought I'd clicked the comment box. Thanks for pointing that out.

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

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

Related Questions

an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers

simple rotation question c# local/world 2 Answers

Move object on local axis instead of world axis after rotation 0 Answers

rotate vector around local axis 2 Answers

Move object relative to its axis 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