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 chalkesonali · May 08, 2014 at 12:43 PM · collisionrotation axis

rocket passing rings

I am creating a simple game in unity 3d where there is a rocket and some rings that will be generated randomly and the rocket has to pass through the rings using the left and right buttons which will turn the rocket left and right.

i have used this script to generate random rings,this is attached to the ring prefab.

 public GameObject ring;
     
     float counter = 50f;
     GameObject clone;
 
     void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player" && this.gameObject.tag=="ring") 
                   {
             Quaternion rot=Quaternion.Euler(0,Random.Range(0,360),0);
             Vector3 v = new Vector3 (this.transform.position.x,                      this.transform.position.y, this.transform.position.z + counter);
             clone=Instantiate (ring, v, rot) as GameObject;
             Destroy(this.gameObject);    
               }
     }

this script places rings one after the other keeping a distance of 50 in between when the rocket passes through the collider and the y axis is given a random rotation.

But if the ring gets a random rotation of eg:90 then when the rocket passes through the ring goes to another direction and the next ring that is generated is on another direction.how to should i find the rotation of previous ring and generate a rotation so that the rocket can see the ring or is there any other way to do this?

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

2 Replies

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

Answer by robertbu · May 08, 2014 at 03:48 PM

I'm assuming this is a 3D game. In visualizing your game, I think what you want to do is Instantiate() your ring somewhere in front of the rocket where the ring can be seen regardless of the direction the rocket is facing. Try this:

     public GameObject ring;
 
     float counter = 50f;
     float maxOffset = 10f;
     float maxRotation = 75f;
 
     void OnTriggerExit(Collider other)
     {
         Vector3 forward = GameObject.FindWithTag ("Rocket").transform.forward;
         Vector3 pos = transform.position + forward * counter;
         Quaternion q = Quaternion.LookRotation(forward);
         GameObject clone = Instantiate(ring, pos, q) as GameObject; 
         clone.transform.Translate (Random.Range (-maxOffset, maxOffset), Random.Range (-maxOffset, maxOffset), 0F);
         clone.transform.Rotate (Random.Range (-maxRotation, maxRotation), Random.Range (-maxRotation, maxRotation), 0F);
         Destroy(gameObject);    
     }


Note, I'm assuming your ring is on a Quad. If it is a physical object, then change the rotation line to use negative forward:

 Quaternion q = Quaternion.LookRotation(-forward);

This code works by first Instantiating the ring directly in front of the ship with a rotation that directly faces the ship. Then after it is created, the ring is moved on it local coordinates to offset the ring, and rotated on it local coordinates. You will have to play with 'maxOffset' and 'maxRotation' to see what is right for your game.

Comment
Add comment · Show 1 · 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 chalkesonali · May 09, 2014 at 04:52 AM 0
Share

robertbu your solution works perfect for my game.

avatar image
0

Answer by Owen-Reynolds · May 08, 2014 at 03:24 PM

To find a spot 50 in front of you, use Vector v = transform.position + transform.forward*50;.

But it seems more pen&paper work might be needed for this. Should the new ring maybe be kind of in front of them, and kind of facing them? Random 0-360 spin means the new ring can be sideways to you? Does it matter which way they go through? (since the new ring will be behind them if they go through backwards.) Wouldn't the game be better if they could always see two rings ahead, to plan?

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

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

Related Questions

transform.forward does not work with Rotate(Vector3.up) 0 Answers

Rotation from blender to unity for animated character? 1 Answer

Collider not continuing to Ignore CharacterController after center changes. 0 Answers

Can't detect isgrounded? Help 1 Answer

How to rotate towards a target but only on the y axis ? 1 Answer


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