- Home /
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
Answer by Wolfram · May 23, 2011 at 10:41 PM
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.
Humm, thats still not doing it - its just shooting in the same point no matter what
Still not working. Basically - Ins$$anonymous$$d of the raycast just firing forward, I want it to fire forward as well as moving up or down on Y on 'target'.
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?
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'
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220612083639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast not working correct 0 Answers
Lerp isn't completing itself 3 Answers
How to stop a unit from rotation once it gets to designated position? 2 Answers