- Home /
Setting rotation parallel to ground
I'm making a game where you graffiti on walls. My current code aligns the instantiated decal to the normal of the object being sprayed on, however often it appears upside-down or sideways, so I want to make it align to the ground so it is facing 'up'. I also want to add functionality to rotate the graffiti around the normal by moving the mouse left and right, but my priority is making it appear the right way up.
function Spray()
{
var hit: RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit))
Debug.DrawLine(transform.position,hit.point,Color.red,1.0f);
{
if (hit.transform.tag == "Surface")
{
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
gernz = Instantiate(tagged,hit.point+(hit.normal*0.0001),rot);
Debug.Log("hehe");
}
}
}
Answer by robertbu · May 15, 2014 at 05:32 AM
Shocked. Graffiti on walls? Next thing you know they'll be writing a game about stealing cars! ;)
Untested, but if you are writing on walls (or mostly vertical surfaces) you can do:
Use a Quad (or a Sprite) for the graffiti texture.
Replace line 9 with the following line of code:
var rot = Quaternion.LookRotation(-hit.normal);
Not only on walls, but on trains too. Also, you sell meth to get money for paint cans :)
Thanks, that seems to be on the way to what I'm after. I'm not familiar with Quads and Sprites, so I'm not sure if what is happening now is a result of using a plane prefab with the decal texture on it.
A Quad is a two-triangle, vertical plane. You create one by:
GameObject > Create Other > Quad
So you want your game object that use use for your graffiti textures to be Quads, not Planes.
The code I posted won't work with a plane. If you really want a plane for some reason, you can use an empty game object as a parent and rotate the visible child object so that the graffiti is vertical when the empty parent's rotation is (0,0,0).
Note that I ran a quick test of the Quaternion.LookRotation(), and it worked. The reason it works is there is an optional second parameter that defines 'up' for the LookRotation(). By default it is Vector3.up.
Your answer
Follow this Question
Related Questions
Instantiate not shooting in right direction 1 Answer
transform position and rotation of instantiated object 1 Answer
Making a particle effect parallel to the slope of a terrain 1 Answer
A way to instantiate an item with 90 Degree increments? 2 Answers
Make an instantiated object match the slope of the terrain 1 Answer