- Home /
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?
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.)
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!
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.
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.
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.
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)
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
![](https://koobas.hobune.stream/wayback/20220613105742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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