Help with bullet going in the dirrection fired from view of camera??
so here is my shooting script i have it set to shoot forward but im having a hard time getting it to move the direction the camera is facing/ even have it fire from the camera direction view.
var shotSound : AudioClip; // drag a shot sound here, if any var bloodPrefab : GameObject; // drag the blood prefab here, if any var sparksPrefab : GameObject; // drag the sparks prefab here, if any var crosshair : Texture2D;
function Start () {
//Screen.showCursor = false;
}
function OnGUI () {
GUI.DrawTexture(Rect(Screen.width/2 - 35,Screen.height/2 - 33,64,64), crosshair);
}
function Update () { if(Input.GetMouseButtonDown(0)){ GetComponent.().RPC("shoot", RPCMode.All); } }
@RPC function shoot () { if (shotSound) GetComponent.().PlayOneShot(shotSound); // play the shot sound var hit: RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit)) { // prepare rotation to instantiate blood or sparks var rot = Quaternion.FromToRotation(Vector3.up, hit.normal); if (hit.transform.tag == "Player"){ // if enemy hit... if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed... //hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health hit.transform.GetComponent.().RPC("ApplyDamage", RPCMode.All, 5); } else { // otherwise emit sparks at the hit point if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); } } }
thanks for any help in advance..
Answer by Inok · Sep 28, 2015 at 01:30 AM
Ok look how i make my weapon aim system (that i use in my project):
if (Physics.Raycast(Eyes_Transform.position, transform.forward, hit, Ray_Lenth_Max)) // this ray shoot from character's "eyes" forward to get point where we need focus our weapon's fire.
{
Focus_Point = hit.point;
}
else
{
Focus_Point = Ray.GetPoint(Ray_Lenth_Max);
}
//-------------------------------------------------------------------
dir Vector3 = (Focus_Point - Hands_Transform.position);
if (Physics.Raycast(Hands_Transform.position, dir, hit_2, Range)) // this ray shoot from character's hands (position where weapon mounted) to focus point
{
put here laser based weapon code
}
If you need ballistic shot just use "dir" as direction when you assign force to bullet. Hope this is what you need for good aiming.