x


A Helping Hand With Raycasting

Hello,

Basically, I have a raycast system that shoots out a raycast directly forward. The problem I'm having is that I don't want it to fire directly straight from the transform. I want it to be able to fire at a target but only on the Y axis.

Here is what I'm using:

    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;
    var localOffset = transform.position + (transform.up) * adjustRaycast;
    var layerMask = 1 << 10;
    layerMask = ~layerMask;
    if (Physics.Raycast (localOffset, direction, hit, 90000, layerMask) && MachineGun == true) { //direction
        Debug.DrawLine (localOffset, hit.point, Color.cyan);

So now - its firing straight. But I want it to be able to fire straight at a target but be able to move up or down on the Y axis (if the target is higher than the raycast).

Ive tried changing the var direction to:

var direction = target.transform.position;

But it appears to be firing at the same spot no matter what.

Can someone help me out please.

Cheers

more ▼

asked May 23 '11 at 10:33 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 253

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Your direction needs to be a difference between two vectors, not the target position itself. Try this:

var positionUsingTargetHeight = transform.position;
positionUsingTargetHeight.y=target.transform.position.y;
// now positionUsingTargetHeight contains the starting point you're looking for

direction = target.transform.position - positionUsingTargetHeight;

As a side note, instead of transform.TransformDirection(Vector3.forward) just use transform.forward.

EDIT: Unless I misinterpreted your question. If you want a ray from your current position to the target's position, simply use target.transform as the starting point, and:

var direction = target.transform.position - transform.position;

as the direction.

more ▼

answered May 23 '11 at 10:41 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

Humm, thats still not doing it - its just shooting in the same point no matter what

May 23 '11 at 10:44 PM oliver-jones

Still not working. Basically - Instead of the raycast just firing forward, I want it to fire forward as well as moving up or down on Y on 'target'.

May 23 '11 at 10:50 PM oliver-jones

Hm, I'm still not sure I understand your question correctly. Does the player control this up/down movement? Or do you want the ray to always fire straight, but have its height automatically adjusted so that it reaches the target's height if the player is facing the target?

May 23 '11 at 11:08 PM Wolfram

Your second option - thats exactly what I want 'ray to always fire straight, but have its height automatically adjusted so that it reaches the target's height if the player is facing the target'

May 23 '11 at 11:10 PM oliver-jones

In that case, your starting point would be transform.position, and your direction is:

var direction = target.transform.position - transform.position;
direction-=transform.right*Vector3.Dot(direction,transform.right);
May 23 '11 at 11:15 PM Wolfram
(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:

x1525
x884
x243

asked: May 23 '11 at 10:33 PM

Seen: 1031 times

Last Updated: May 23 '11 at 11:28 PM