Differing Vector for Raycast Based on Inside or Not

Discuss modding questions and implementation details.
Post Reply
l3lessed
Posts: 1355
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Differing Vector for Raycast Based on Inside or Not

Post by l3lessed »

So, I'm trying to finish the last big issue with my ambidexterity mod, but I'm completely stumped. I found a hacky work around, but I need to know what is happening, as the work around is not ideal at all.

The issue is with my physical weapon feature. This feature uses a coroutine to begin shooting out raycasts at a certain point in the animation and then stop at a certain point in order to allow a raycast arc to be made that follows the weapon visually. I have raycasts being shot out every frame. After each raycast, it uses a vector3 lerp and the current running time of the attack animation to move the raycast over a certain amount. It does this until it hits the end vector3 offset point and attack end time to create a nice arc that follows the weapon animation.

The issue is the lerp calculator seems to be pumping out different vector3 positions based on if I'm inside a location or outside. I can't see any code that should be affected by this.

Heres the basic ray calculation code:

Code: Select all

//gets forward facing vector using player camera.
attackcast = GameManager.Instance.MainCamera.transform.forward;

//sets a Quaternion angle for adjusting arccast by calculating the angle of my forward look.
objectRotation = Quaternion.LookRotation(attackcast);

//lerps through lerp cordinates to make arc cast. Locks y position to main camera z rotation(look up/down angle/radian).
attackcast = Vector3.Lerp(new Vector3(110f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), new Vector3(-90f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), percentagetime);

//rotate the attack cast based on players current forward camera position. Ensure raycasts always rotate properly with player
attackcast = (objectRotation * attackcast);

//Grab the players position for start of ray plus any offset vectors passed through (I'm not using any offsetting in my code)
Vector3 startPosition = GameManager.Instance.MainCamera.transform.position + offset;

//shoot the ray from players camera to the end vector distance and position
Ray ray = new Ray(startPosition, attackcast);
The moment I go into an outside area, the raycast arc becomes much smaller and the lerp arc being covered is clearly much much smaller. When I look at a log of the vector positions, it clearly is covering less distance in the attack arc when I'm outdoors.

Here is the gimmick I used to get around it for now and it produces almost the same arc inside or out, but as you can tell, it requires insanely high vector3 differences to produce the same raycast arc by the end of the attack/lerp calculation.

Code: Select all

if(GameManager.Instance.IsPlayerInside)
	attackcast = Vector3.Lerp(new Vector3(-90f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), new Vector3(110f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), percentagetime);
else
	attackcast = Vector3.Lerp(new Vector3(-720f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), new Vector3(720f, GameManager.Instance.MainCamera.transform.rotation.z, GameManager.Instance.MainCamera.transform.position.z), percentagetime);
Any idea what would be causing this weird behavior between the vector3 lerping distance inside versus outside?
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

l3lessed
Posts: 1355
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Differing Vector for Raycast Based on Inside or Not

Post by l3lessed »

I found the fix.

I can't explain what exactly the issue was, as it involves math way above my head. However, it was clearly an issue how the vector and rotations were being calculated. Something about being inside versus outside was causing the calculations to change. Possible the euler calculation method I was using and applying to the vector.

Going for a simpler approach of using a math float lerp calculator and plugging it into an angle axis calculator with the proper rotation center transform worked to find the proper rotation. Then multiplying it by a transform forward fixed it.

Code: Select all

float XAngleCast = 0;
float yAngleCast = 0;

switch (weaponState)
{
	case WeaponStates.StrikeRight:
	//lerps through arc degrees to make an arc ray cast.
	XAngleCast = Mathf.Lerp(-110, 110f, percentagetime);                                        
	//rotates vector3 position  using above lerp calculator then shoots it forward.
	attackcast = Quaternion.AngleAxis(XAngleCast, GameManager.Instance.MainCamera.transform.up) * GameManager.Instance.MainCamera.transform.forward;
	break;
}
hitObject = AmbidexterityManager.AmbidexterityManagerInstance.AttackCast(equippedAltFPSWeapon, attackcast, new Vector3(0, 0, 0), out attackHit, weaponReach);
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

Post Reply