- Home /
Bullet Hole sprites don't align correctly on walls
They seem to be aligning almost at random:
I'm not quite sure why this isn't working correctly, I'm rather new to Unity and C# as well.
Here is my code for it:
/*
* Shooting
*/
float trigger = Input.GetAxis ("Fire1"); // Gets mouse left button input
if (timeToShoot <= 0.0f && trigger > 0) {
triggerShot = true;
timeToShoot = 0.3f;
RaycastHit hit;
Ray lineOfSight = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Physics.Raycast(lineOfSight, out hit);
var hitRotation = Quaternion.FromToRotation(Camera.main.transform.forward, hit.normal);
Instantiate(bullethole, hit.point, hitRotation);
print (hit.collider.tag);
}
if (triggerShot) {
timeToShoot -= Time.deltaTime;
//flare.light.enabled = true;
if (timeToShoot <= 0) {
//flare.light.enabled = false;
triggerShot = false;
}
}
/*
* END Shooting
*/
Answer by nesis · Feb 01, 2014 at 03:28 AM
The rotation you are getting is the rotation required to go from your camera's forward vector to the normal vector of the wall you hit. What you want to use is something like Quaternion.SetLookRotation(hit.normal,Random.rotation);
Answer by robertbu · Feb 01, 2014 at 04:04 AM
Assuming your bullet hole is a sprite, then I think you what this (untested):
var hitRotation = Quaternion.FromToRotation(Vector3.back, hit.normal);
Instantiate(bullethole, hit.point, hitRotation);
Here is a possible alternate:
var go = Instantiate(bullethole, hit.point, Quaternion.identity) as GameObject;
go.transform.forward = -hit.normal;
Your answer
Follow this Question
Related Questions
C# OnCollisionEnter() problem 0 Answers
how to make a bullet reflect off a wall? 4 Answers
Bullet Not Working 3 Answers
Issues with bullet not subtracting properly 1 Answer
How to add impact sparks / blood effects to 2D shooter? 2 Answers