Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast hit.point issue

Discussion in 'Scripting' started by FisherM, Aug 8, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Hey I am creating a ballistics system, however I have an issue with the Raycast Hit.
    The hit.point is always landing in the incorrect position. Here is an image.

    There is a sphere showing the trajectory of the bullet floating in the air. there is also a sphere being instantiated on the ground and the hit.point as you can see the sphere on the ground is not where the line hits the ground.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class projectile2 : MonoBehaviour {
    6.  
    7.     public float velocity;
    8.  
    9.     Vector3 Next_Position;
    10.     GameObject Hit_Object;
    11.     Vector3 Hit_Normal;
    12.     Transform trns;
    13.     float dwnVel;
    14.     public float mass;
    15.     float g = 9.81f;
    16.     public GameObject marker;
    17.     public float velocityLoss;
    18.     public GameObject Explosion_Object;
    19.     void FixedUpdate(){
    20.         // saves the next position
    21.         Vector3 Vec3 = trns.position + trns.forward * velocity * Time.fixedDeltaTime + Vector3.down * dwnVel * Time.fixedDeltaTime;
    22.  
    23.         // Spawns the marker from the bullet generator along the trajectory
    24.         GameObject trajmark = (GameObject) Instantiate (marker, trns.position, trns.rotation);
    25.         drawline linemarker = trajmark.gameObject.GetComponent<drawline>();
    26.         linemarker.setup (trns.position + trns.forward * velocity * Time.fixedDeltaTime + Vector3.down * dwnVel * Time.fixedDeltaTime);
    27.         // //
    28.  
    29.         // raycasts to test for collision
    30.         RaycastHit hit;
    31.         if (Physics.Raycast (trns.position, Vec3, out hit)) {
    32.             Debug.Log("HIT" + hit.point);
    33.             //trns.position = Vec3;
    34.             Instantiate ( marker , hit.point , Quaternion.identity ) ;
    35.             Destroy (this.gameObject);
    36.         } else {
    37.             trns.position = Vec3;
    38.         }
    39.         // //
    40.  
    41.         // increase the downward velocity
    42.         dwnVel += (mass * g);
    43.         if (velocity > 0.0f) {
    44.             velocity += velocityLoss * Time.fixedDeltaTime;
    45.         } else {
    46.             velocity = 0.0f;
    47.         }
    48.         // //
    49.     }
    50.  
    51.     // Use this for initialization
    52.     void Start () {
    53.         trns = this.transform;
    54.  
    55.     }
    56. }
    57.  
    58.  
     
    Last edited: Aug 8, 2014
  2. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Added comments to help make it more readable, the issue is in the raycast but it uses the same Vec3 variable so I have no idea why this issue is occuring?
     
  3. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Still really need some assistance with this have got none so far and I've been wrestling with it for more than a day now
     
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    You use the end point "Vec3" as a direction vector for you Raycast, you should use the difference with the current position to get the actual direction:

    Code (csharp):
    1.  
    2. if (Physics.Raycast(trns.position, Vec3 - trns.position, out hit))
    3.  
    Does it helps ?
     
  5. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    That solved the issue thanks so much

    R