Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision on attack function (using Raycast)

Discussion in 'Scripting' started by Luca91, Jul 14, 2014.

  1. Luca91

    Luca91

    Joined:
    Apr 13, 2013
    Posts:
    75
    Hi all,
    I've this attack function:
    Code (csharp):
    1.  
    2. IEnumerator Attack(){
    3.         yield return new WaitForSeconds(2.0f);
    4.  
    5.         RaycastHit hit ;
    6.        
    7.         Vector3 targetDir = target.position - transform.position;
    8.         var forward = transform.forward;
    9.         var angle = Vector3.Angle(targetDir, forward);
    10.         Debug.Log(targetDir);
    11.    
    12.         if (Physics.Raycast(transform.position, targetDir, out hit, 3.0f)) {
    13.            
    14.             if (hit.rigidbody){
    15.                 hit.rigidbody.AddForceAtPosition(10 * targetDir / (Time.fixedDeltaTime * 100.0f), hit.point);
    16.             }
    17.  
    18.             hit.collider.SendMessageUpwards("damage", Random.Range(30, 35), SendMessageOptions.DontRequireReceiver);
    19.             Debug.Log("Collider: "+hit.collider);
    20.         }
    21.         yield return new WaitForSeconds(animation["First_Attack"].length - delayShootTime + Random.Range(0.0f, 0.30f));
    22.  
    23.     }
    24.  
    The problem is that the reycast doesn't seems to hit my player. What I'm doing wrong ?
    If I put 100.0f as distance, the reycast hit the floor.

    Thanks for the help
     
  2. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Why don't you just use transform.forward in your raycast? Instead of targetDir? That'll give you a weird angle for the raycast to be coming out at
     
  3. Luca91

    Luca91

    Joined:
    Apr 13, 2013
    Posts:
    75
    I'm using transform.forward now, and I'm also using Debug.DrawLine, but the debug line is not forward from the transform, it is completely wrong, why ?
     
  4. Luca91

    Luca91

    Joined:
    Apr 13, 2013
    Posts:
    75
    Solved, it was Debug.DrawRay :)