0
\$\begingroup\$

Player cam has a raycast. I need it to work as pointer for player's bullet direction. If the ray hits any kind of object this spot will be the aim of player's bullets.

enter image description here

Here is my script

public class camraycast : MonoBehaviour {

Camera mycamera;
GameObject prefab;

public int bulletSpeed = 10;
public GameObject angle; //  camera 
public GameObject players;


void Start () {
    prefab = Resources.Load("bullets") as GameObject;
    mycamera = GetComponent<Camera>();
}

void Update() {

    if (Input.GetMouseButtonDown(0))
    {
        Vector3 camScreen = new Vector3(0.5f, 0.5f, 0f); // center of the screen
        float rayLength = 100f;
        Ray ray = mycamera.ViewportPointToRay(camScreen);
        RaycastHit hit;
        Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);

        if (Physics.Raycast(ray, out hit, rayLength))
        {
            print ("Looking at : " + hit.transform.name);
            GameObject bullets = Instantiate(prefab) as GameObject;
            bullets.transform.position = players.transform.position + angle.transform.forward * 0.1f;
            Rigidbody rb = bullets.GetComponent<Rigidbody>();
            rb.AddForce((hit.point - angle.transform.position) * bulletSpeed);
            Destroy(bullets, 9f);
        }
    }
    Vector3 camScreens = new Vector3(0.5f, 0.5f, 0f); // center of the screen
    float rayLengths = 100f;
    Ray rays = mycamera.ViewportPointToRay(camScreens);
    Debug.DrawRay(rays.origin, rays.direction * rayLengths, Color.red);
}
}
\$\endgroup\$
  • \$\begingroup\$ i think you don't need to use hybrid of raycast and bullet object, no of course you can if bullet will be slow, but it won't work if bullet travels fast, i achieved bullet physics with only raycast like: bullet drop, recoil etc. I recommend you trying this. but still, it's your choice. \$\endgroup\$ – Nick Sep 15 '16 at 6:15
  • \$\begingroup\$ I have tried many things and no good result. \$\endgroup\$ – user43474 Sep 15 '16 at 18:30
  • \$\begingroup\$ since it's hard to implement all ballistics in raycast shooting, i think you can start from bullet drop, for eg: we know bullet mass and it's speed, thus we know it's drop, (for start let's say we have constant drop so it's more easy) so you move your raycast down every meter by the bullet drop distance(on every meter). i did something like that and it worked. but then there are things like, raycast does this immediately, so you need timer when damage raycast to appear. (distance/speed) it is when damage raycast appears. \$\endgroup\$ – Nick Sep 16 '16 at 11:11
1
\$\begingroup\$

Most FPS games just use HitScanning, where they fire a ray out the centre of the screen and get back the first object it hits, applying logic to that object. Any bullets coming out of the weapon itself are just graphical assets and don't actually interact with anything.

If, however, you do really want to simulate the physics of bullets, then what you can do is add in the line

bullets.transform.LookAt (hit.transform);

after you instantiate but before you add the force and it should make the bullet rotate to look directly at the object that the raycast hit, before you fire it off.

\$\endgroup\$
  • \$\begingroup\$ I tried and nothing change. ` GameObject bullets = Instantiate(prefab) as GameObject; Rigidbody rb = bullets.GetComponent<Rigidbody>(); bullets.transform.LookAt (hit.transform); rb.AddForce((hit.point - angle.transform.position) * bulletSpeed); Destroy(bullets, 9f); ` I found a tutorial on unity page. unity3d.com/learn/tutorials/projects/lets-try-assignments/… Have the same idea. I tried to match it with my code but i couldn't ! it use laser "line-renedr" and i use bullet - \$\endgroup\$ – user43474 Sep 16 '16 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.