Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Silicon Power on March 21, 2024, 07:16:13 AM

Title: How can I align the gun’s crosshair with an object in the scene?
Post by: Silicon Power on March 21, 2024, 07:16:13 AM
Hello, everyone. I need some help with this issue. I have a 3D crosshair in front of my gun, but it passes through walls and objects. So I need a 2D crosshair to solve this problem, but I’m not sure how to align the 2D crosshair with the object that’s in front of my gun. Any advice would be appreciated
Title: Re: How can I align the gun’s crosshair with an object in the scene?
Post by: Silicon Power on March 21, 2024, 07:50:01 AM
Also I asked AI to help me but the code provided by ai doesn't work

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

public class Align2DImageWith3DObject : FsmStateAction
{
    public FsmObject image2D;
    public FsmObject object3D;
    public FsmObject mainCamera;

    public override void OnUpdate()
    {
        // Convert the 3D object's position to screen space.
        Vector3 screenPosition = ((Camera)mainCamera.Value).WorldToViewportPoint(((GameObject)object3D.Value).transform.position);

        // Convert the viewport position to a position that can be used for the RectTransform.
        Vector2 proportionalPosition = new Vector2(screenPosition.x * Screen.width, screenPosition.y * Screen.height);

        // Update the 2D image's position.
        ((RectTransform)image2D.Value).localPosition = proportionalPosition;
    }
}
Title: Re: How can I align the gun’s crosshair with an object in the scene?
Post by: Silicon Power on March 27, 2024, 01:37:17 PM
.
Title: Re: How can I align the gun’s crosshair with an object in the scene?
Post by: djaydino on March 28, 2024, 08:47:08 AM
Hi.
Can you give more details.
Are you in 1st/3rd person.

can you maybe show in a video how it works in 3d version.
Is it just in from of the player, does it auto target.
does it change distance when aiming to something?
Title: Re: How can I align the gun’s crosshair with an object in the scene?
Post by: Silicon Power on March 28, 2024, 10:54:45 AM
Hi.
Can you give more details.
Are you in 1st/3rd person.

can you maybe show in a video how it works in 3d version.
Is it just in from of the player, does it auto target.
does it change distance when aiming to something?

Hi, thank you. I only need an action that can match the X and Y position of a 2D image within a canvas to a 3D object in the scene.