x


Problem raycasting to object colliding to child

Hours have passed and I can't get this to work. I have a object with a empty object with box collider as a child so that I can get a sort of line of sight area, but I'm trying to get a raycast to be shot from the character to another character so that it can check for line of sight and it won't work.

Whatever I try to do, the raycast only goes to the same direction which is 3 o'clock from the object that sends the raycast. Wth am I doing wrong?

var losRange : Vector3;
var hit : RaycastHit;
var projectile : GameObject;
var projectileSpeed = 300.0;
var projectileLifespan = 2;
var attackRange = 1.0;
var lostrigger : LoSTrigger;
lostrigger = GetComponent(LoSTrigger);

function Start()
{
    animation.wrapMode = WrapMode.Loop;    
    animation.Play("idle");
}

function Update(){

    Debug.DrawRay(transform.position, lostrigger.losTarget.position, Color.blue);

    LookAtIgnoreHeight(lostrigger.losTarget);
    Debug.Log("attackonsight");
    if (Physics.Raycast(transform.position, lostrigger.losTarget.position, hit, attackRange)) {
        print("Blocked by " + hit.collider.name);
        AttackonSight();
    }
}

function AttackonSight(){

    if (Vector3.Distance(transform.position, lostrigger.losTarget.position) < attackRange)
    {
      EnemyAttack();
        //cooldown = fireRate;
    }
    else
    {
       EnemyMovement();
    }
}
more ▼

asked Aug 15 '12 at 09:05 PM

marcelomedeiros gravatar image

marcelomedeiros
3 1

Do you want to send a Ray from point A to B?

Aug 15 '12 at 09:19 PM NhommeBeurreOne

I wanna send a ray from gameobject1 to the gameobject2 that collided with a specific box collider

Aug 15 '12 at 09:23 PM marcelomedeiros
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
 if (Physics.Raycast(transform.position, (lostrigger.losTarget.position - transform.position), hit, attackRange)) {

etc..

same thing with the DrawRay, you need a direction, not a position

more ▼

answered Aug 15 '12 at 10:55 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

Oh wow.. thanks, it worked. I gotta say maybe I need to get some sleep but I didn't quite get how's that different from having just one of those positions as a direction. Didn't you just subtract one position from another?

Aug 16 '12 at 01:52 PM marcelomedeiros

what you need to understand is that a Vector3 can be used to represent both positions AND directions.. take for example

Vector3(0,1,0)

as a POSITION, this is 1 unit above the world origin, right near the center of the world

as a direction, this is straight up (no matter what the position)

so lets say my player is at some other position(say(1,1,1)), and I want the direction to my object at (0,1,0)...

saying:

if (Physics.Raycast(player.transform.position, Vector3(0,1,0) , hit, attackRange)) {

means I want a ray starting at the player, in the direction straight up! but that's not right, I wanted the direction from my player to the target. Saying:

(targetPosition - originPosition) gives us the relative direction, which is what the Ray uses (a DIRECTION, not a POINT)

(0,1,0) - (1,1,1) = (-1,0,-1)

in this case, the direction from my player to my target is Vector3(-1,0,-1).. this is quite different from (0,1,0)!

Aug 16 '12 at 02:12 PM Seth Bergman

You sir, are the man. Thanks again!

Aug 16 '12 at 02:45 PM marcelomedeiros
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1525

asked: Aug 15 '12 at 09:05 PM

Seen: 221 times

Last Updated: Aug 16 '12 at 02:45 PM