Home > Mobile >  Unity rotation calculation
Unity rotation calculation

Time:06-23

    //RAYCAST
    RaycastHit hit = new RaycastHit();
    if(Physics.Raycast(impact.position, bloom, out hit, 1000f, canBeShot))
    {
        GameObject newBulletHole = Instantiate(bulletHolePrefab, hit.point   hit.normal * 0.001f, Quaternion.identity) as GameObject;
        newBulletHole.transform.LookAt(hit.point   hit.normal);
        Destroy(newBulletHole, 5f);
    }

    //Bullet
    bulletSpawnPoint = GameObject.Find("BulletSpawn").transform;

    var bullet = Instantiate(bulletPrefab, bulletSpawnPoint.position, **bulletSpawnPoint.rotation**);
    bullet.GetComponent<Rigidbody>().velocity = bulletSpawnPoint.forward * loadout[currentIndex].bulletSpeed;

I need to get the perfect "bulletSpawnPoint.rotation" depending of my bullet hole created by the raycast hit. Thanks

enter image description here

CodePudding user response:

So you want to spawn the bullet and then use Rigitbody to move it to the newBulletHole gameobject.

Easy way would be to store position of the bullet hole and then pass it into LookAt method of the bullet Transform before adding velocity.

A bit more advanced way (and this is what you are asking in your question, afaik): calculate the direction vector (bulletSpawnPoint.transform.position - newBulletHole.transform.position) and then get Quaternion (i.e. rotation) with LookRotation

  • Related