- Home /
how to rotate bullet towards target direction?unity2D
i've problem to rotate bullet towards in enemydirection in unity 2D.is there another way to do this instead of lookrotation?(i think this works only in unity 3D , because for rotating in unity2d we just change the z axis but when i debug Quaternion.lookrotation(dir) i've found the x, y, and w also had values so in vector.slerp the current rotation will changing smoothly to that!) here is my code.
void Update() { if (targetposition != null) { var dir = (targetposition.position - transform.position); Quaternion _rotation = Quaternion.LookRotation(dir , Vector3.forward); Quaternion _current = transform.localRotation; transform.rotation = Quaternion.Slerp(_current, _rotation, Time.deltaTime );
// transform.position = Vector3.SmoothDamp(transform.position, targetposition.position, ref vel, 0.2f);
}
}
this code is for bullet instances and in enemyshoot i 've just instantiating bullets and pass the targetposition!