- Home /
Bullet slowing down while snapping to sphere normals
I'm having an issue where a bullet will slow down and stop after a short period of time while snapping to surface normals of a sphere. Here is the code I am using:
function Update () {
transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
//transform.position += transform.forward * bulletSpeed * Time.deltaTime;
//Gets rotation object info
var globePos = new Vector3(0,0,0);
initCastDir = globePos - transform.position;
var hit: RaycastHit;
if (Physics.Raycast (transform.position, initCastDir, hit))
{
Debug.DrawRay (transform.position, initCastDir * 10, Color.blue);
transform.position = hit.point;
}
}
I can resolve this if I use this line in the raycast statement:
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
Unfortunately this forces the bullet to travel only upwards and makes it unable to rotate to the transform of the player.
Can anyone shed any light on this?
Your question is puzzling. I'm missing critical information about what you are trying to solve with the line of code of code and why you want the bullet to snap to the normal of the sphere.
so basically I want it to travel around the sphere as the hole game is set on one. Basically I want it to retain its initial transform.rotation and continue along the path the player was facing when the bullet was instantiated.
Answer by robertbu · Aug 19, 2013 at 04:17 PM
So if I understand correctly, you want the bullet to curve around to follow a sphere. You can do it with a raycast as you have done. The line of code you want is:
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
But I'd be a bit concerned about accuracy. That is, while we see a sphere, the object is really a mesh with triangles. Since it is a sphere you can calculate the vector more directly:
var globeUp = transform.position - globePos;
transform.rotation = Quaternion.FromToRotation(transform.up, globeUp) * transform.rotation;
Brilliant. Thats exactly what I needed Roberto. Can you explain what the * transform.rotation does?
what the * transform.rotation does?
It combines rotations. FromToRotation() generates a rotation from the given vector to the vector. You could consider it a relative rotation. The '* transform.rotation' applies that relative rotation to the current rotation of the object.