- Home /
How do I make create a rotateable object that follows a raycast hit point?
So I have created a script detecting the point where the raycast hits, and I have been able to instantiate a prefab to that point, but all I wish to do is to be able to rotate this on both the X and the Y axis, as the object that is going to repeadetly spawn/despawn is rotated 90 degrees on the wrong axis. And I also want to be able to rotate it on the Y axis so that I can later on place it.
Any ideas how I can change the rotation of the Quartenion?
Small gif of how it works right now: http://recordit.co/JPxHUsOk5A
Code used:
var rotation = Quaternion.identity;
Instantiate(bScript.shelterPrefabMarker, hit.point, rotation);
in the update function.
Answer by Harald921 · Jul 22, 2015 at 02:20 PM
If anyone else stumbles upon this, this is how I solved it (C#):
tmp.transform.position = hit.point;
if (Input.GetKey(KeyCode.Q))
tmp.transform.Rotate(0, -rotationSpeed * Time.deltaTime, 0);
else if (Input.GetKey(KeyCode.E))
tmp.transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
Answer by fighder · May 10, 2015 at 08:09 PM
I wouldn't suggest changing the Quaternion, cuz messing around with Quaternion is just not a good idea unless you understand it.
What you can do is the following:
var temp : GameObject = Instantiate(bScript.shelterPrefabMarker, hit.point, Quaternion.identity) as GameObject;
temp.transform.rotate(90,90,90); //rotate your gameObject
I am not too familiar with javascript syntax, but basically you grab the gameObject that will be spawned with type casting, and then rotate the object.
Okay, thanks, I will try it out later when I get home. The reason I need it rotated is because, I use it as a object detecting collision with different building materials, and when all the variables have reached a certain value, the building is instantiated.. 90 degrees in the wrong x rotation. But many thanks!!
Your answer
Follow this Question
Related Questions
Instantiate GameObject towards player 0 Answers
How is the rotation of a Transform converted into a Vector3 in the inspector ? 2 Answers
c# modify only one axis of a quaternion 2 Answers
Turret rotation on one axis problems 2 Answers
Rotating a GameObject based on another GameObject's y-axis 1 Answer