Reference the Camera Transform To Get Aim Direction

Hi everyone! I'm new here to both Core Games and the forums. I wonder if you can help with the following script (as the API documentation is rather vague around the Camera type).

The current script below references the player that owns this script and the related equipment. Currently, it uses the Player transform to get the facing direction and applies the velocity from the facing direction. It works perfectly as intended.

However, obviously the player transform direction doesn't account for Y axis aiming (which is fine). So, instead of using the direction of the player, I would prefer to use the direction of the player's camera. Therefore, if the user aims upward or downward by moving the camera, the 'Facing Vector' would change accordingly.

Therefore, does anyone know how (or if we can) reference a particular players' camera, and if the 'GetWorldTransform()' function would work?

Thanks in advance
Kyle

"On_Throw" Script :

local propBall = script:GetCustomProperty("Ball"):WaitForObject()
local propPhysicsBall = script:GetCustomProperty("PhysicsBall"):WaitForObject()
local propPickupTrigger = script:GetCustomProperty("PickupTrigger"):WaitForObject()
local propVelocity = propPhysicsBall:GetCustomProperty("Velocity")
local ANG_VELOCITY = propPhysicsBall:GetCustomProperty("AngVelocity")
local ability = script.parent


function OnExecute(ability)
local Equipment = script:FindAncestorByType("Equipment")
local EquipmentPos = Equipment:GetWorldPosition()
 
local PlayerHeld = Equipment.owner
local PlayerHeldTransform = PlayerHeld:GetWorldTransform()
local PlayerFacingVector = PlayerHeldTransform:GetForwardVector()


propPhysicsBall.isEnabled = true
propPhysicsBall:SetWorldPosition(propBall:GetWorldPosition())
propBall.visibility = Visibility.FORCE_OFF

Equipment:Unequip()
propPickupTrigger.collision = Collision.FORCE_ON


propPhysicsBall:SetVelocity(PlayerFacingVector * propVelocity)
    propPhysicsBall:SetAngularVelocity(Vector3.UP * ANG_VELOCITY)

   Task.Wait(2)

-- Cut the velocity (and angular velocity) down to 25%
propPhysicsBall:SetVelocity(propPhysicsBall:GetVelocity() * 0.25)
propPhysicsBall:SetAngularVelocity(propPhysicsBall:GetAngularVelocity() * 0.25)
end

ability.executeEvent:Connect(OnExecute)

Hi! I was looking for something similar to perform a Raycast using the Player Camera's look direction, but using player:GetActiveCamera() then finding its WorldTransform didn't seem to provide what I needed. However, you can calculate the forward vector yourself using:

player:GetViewWorldRotation() * Vector3.FORWARD

Full view Raycast code:

local debugSphereObject = script:GetCustomProperty("DebugSphere"):WaitForObject();
local rayLength = script:GetCustomProperty("RayLength");

function Tick()
	local player = Game.GetLocalPlayer();
	
	-- GetViewWorldPosition & GetViewWorldRotation return position/rotation of the player camera.
	local playerViewPosition = player:GetViewWorldPosition();
	local playerViewRotation = player:GetViewWorldRotation();
	
	-- Rotate a forward vector by the camera rotation to get the player camera look direction
	local playerLookDirection = playerViewRotation * Vector3.FORWARD;
	local rayEnd = playerViewPosition + playerLookDirection * rayLength;
	
	-- Raycast and place the debug sphere at the hit location
	local hitResult = World.Raycast(playerViewPosition, rayEnd, {ignorePlayers = true});
	if hitResult then
		local impactPosition = hitResult:GetImpactPosition();
		debugSphereObject:SetPosition(impactPosition);
	end
end

Demo:
Raycast

Yes, worked like a charm! Thanks for the help

It also worked like a charm for me too ! thanks for sharing your knowledge , :blush: :grinning_face_with_smiling_eyes: