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 Knightraven8 · Jul 09, 2013 at 09:36 AM · instantiateobjectvectorwind

How do you instantiate an object to be oriented along a vector3?

I am creating a game where you fire cannonballs. I have created a function in my script that adds a constant force to the cannonballs along a randomized vector3 to simulate wind. I need a way to show the player which way the wind is blowing, so I was thinking I could instantiate a 3D arrow on screen that points which way it blows each time the wind changes, but I'm not sure how to orient the arrow along the axis the constant force vector is currently pointing. Is there an easy way to do this?

Comment
Add comment · Show 2
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 prototype7 · Jul 09, 2013 at 11:03 AM 0
Share

how about decide the arrow direction first and than add constant force base on that arrow points by using relativeForce(The force - relative to the rigid bodies coordinate system - applied every frame.)

avatar image Knightraven8 · Jul 09, 2013 at 11:59 AM 0
Share

Interesting method. I'll have to try it out the next time I get a chance to work on my game. Thanks for the suggestion!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Jul 09, 2013 at 12:50 PM

store the randomized vector then.

WindArrowRotation = Quanternion.SetLookRotation(storedRandomVector);

you can supply it with an up vector but it assumes you want the top of the arrow towards the sky.

If this arrow is going to be on its side to see it make sure it has an edge as up in the modeling program.

If not if you want you can do SetLookRotation(storedRandomVector,player.transform.forward);

or -player.transform.foward so its looking towards the wind with the top looking at you if the actual drawing of the arrow is the top and the edge.

Comment
Add comment · Show 4 · 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 Knightraven8 · Jul 13, 2013 at 02:03 PM 0
Share

I tried to use this method to orient the arrow, but I'm not sure I am doing it correctly. When I passed in the wind's stored vector3, I got an error saying that an instance of type UnityEngine.Quaternion was required for SetLookRotation. I tried to find a way to typecast the vector3 into a quaternion but it didn't seem possible. I'm not too familiar with quaternion functions yet, but from what I was reading online, it has something to do with quaternions storing more information than a vector3 does. Is there a way around this or am I just not understanding the functionality of quaternions vs vector3 correctly? Thanks for the help.

avatar image sparkzbarca · Jul 13, 2013 at 02:49 PM 0
Share

yes windowarrowrotation is a new variable im introducing into your code it is of type quanternion.

you should have

 Quanternion WindowArrowRotation;
 
 WindArrowRotation = Quanternion.SetLookRotation(storedRandomVector);
 
 then your going to have this arrow object itself. I'm going to call the game object WindArrow and the prefab WindArrow Prefab
 
 Public Gameobject WindArrowPrefab;
 GameObject WindArrow;
 Public GameObject Player;
 
 instantiate takes 3 arguments, the prefab to spawn, the position of the spawned object and the rotation. We'll use in this case a position which is 1 meter in front of the player
 
 WindArrow = Instantiate(WindArrowPrefab,Player.transform.position + 1,WindArrowRotation);

Now we have a wind arrow which is facing what should be the correct way 1 meter out in front of the player.

Of course you'd modify this script for your specific needs but that should get you there generally speaking.

avatar image Knightraven8 · Jul 13, 2013 at 04:02 PM 0
Share

Alright, I've added the WindArrowRotation as a Quaternion and tried to pass in my wind vector, but I'm still getting the error. Here is my code:

 var windArrow : GameObject;
 var windArrowRotation : Quaternion;
 private static var wind: Vector3;
 
 function setWind() {
     wind = new Vector3(Random.Range(-100.0,100.0),Random.Range(-50.0,50.0),Random.Range(-100.0,100.0));
     windArrowRotation = Quaternion.SetLookRotation(wind);
     windArrow = Instantiate(windArrow, initPos, windArrowRotation);
     logVector("New wind:", wind);
 }

When I compile the code, the line windArrowRotation = Quaternion.SetLookRotation(wind); throws up an error saying an instance of UnityEngine.Quaternion is required to access non static member 'SetLookRotation'. (BCE0020)

avatar image Knightraven8 · Jul 13, 2013 at 11:43 PM 0
Share

O$$anonymous$$ so I was able to get the code working by just using windArrowRotation = Quaternion(wind.x,wind.y,wind.z,0);

So far it seems to be working. Thanks again for the help.

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

Lock rotation of an instantiated object to another 1 Answer

Everytime I Instantiate, object becomes smaller. 2 Answers

Have a Timer Counts To 5 sec. Then Make a Sphere Appear While Using Instantiate 1 Answer

Add a wind vector to local velocity for calculating wind speed 2 Answers

Instantiating droppable items on destroy game object 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