Hi there.
Right now I’m trying to get a raycast to always aim straight ahead of it’s origin point, but be able to angle up and down on a single axis depending on where a separate object is located.
I’ve thought I had it numerous times, but I seem to be pulling up just short of a solution. I’m using DrawLine to see what I’m doing for the moment. Here’s my current attempt, with all the irrelevant stuff cut out:
var weaponFocus = bulletTarget.transform.position; //This is the object the ray should be following.
var rayCastPos = weaponRifle.transform.position;
var weaponDirection = weaponRifle.TransformDirection(Vector3.up); //The model I'm using has it's up position at the end of the barrel, so Vector3.up is actually forward. Woops.
var weaponFocusLimitedX = weaponDirection.x;
var weaponFocusLimitedY = weaponDirection.y;
var weaponFocusLimitedZ = weaponDirection.z;
Debug.DrawLine(rayCastPos,Vector3(weaponFocusLimitedX,weaponFocusLimitedY,weaponFocus.z).normalized * 100, Color.red,0.0,true);
The basics of what I’m trying to accomplish here is to have the ray head straight forward from the weapon, but be able to move up and down to follow a target. I don’t want it following the target left or right, however.
Any help with this is greatly appreciated.