#ScreenToWorldPoint

1 messages · Page 1 of 1 (latest)

green swallow
#

Hello, im very new in comparison to alot of the people here.
prepare for some turbo-noob question.

Having troubles with 2D and i believe my solution involves ScreenToWorldPoint.
I for the most part know what i want to write to create a weapon that orbits my character, which rotates towards the cursor.

Currently having a bit of confusion..

For WASD And Spacebar I have been using Input.GetKeyDown(KeyCode.Space)
and the inputmanager

Looking for a really simple and short way to capture a screen position under the mouse-cursor into a Vector2!

my solution that DID not work was:

       private WeaponParent weaponParent;
[SerializeField]
        private InputActionReference movement, attack, pointerPosition;


 void Start()
    {

        weaponParent = GetComponentInChildren<WeaponParent>();
        
    }

 pointerInput = GetPointerInput();
        weaponParent.PointerPosition = pointerInput;

  private Vector2 GetPointerInput(){ ///bs for pointer
        Vector3 mousePos = pointerPosition.action.ReadValue<Vector2>();
        mousePos.z = Camera.main.nearClipPlane;
        return Camera.main.ScreenToWorldPoint(mousePos); ///who tf is screen to world and why are they pointing


    }

and subsequently when adding this code to my script my character can no longer move.

#

here is the total script with my attempted solution present

#
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovementDynamic : MonoBehaviour
{

        public float moveSpeed;
        public float moveSpeedDefault = 3.5f;
        public Rigidbody2D rb2d;
        private Vector2 moveInput;
        public Animator animator;

        private Vector2 pointerInput;

        private WeaponParent weaponParent;


        [SerializeField]
        private InputActionReference movement, attack, pointerPosition;




        // the movement speed set to the player during an attack animation SEE LINE 43 -> 48
        public float lungeDistance = 0f;
        





        
    // Start is called before the first frame update
    void Start()
    {

        weaponParent = GetComponentInChildren<WeaponParent>();
        
    }

    // Update is called once per frame
    void Update()
    {
        
        ///bs for pointer
        pointerInput = GetPointerInput();
        weaponParent.PointerPosition = pointerInput;

        ///
        moveInput.x = Input.GetAxisRaw("Horizontal");
        moveInput.y = Input.GetAxisRaw("Vertical");
    
        animator.SetFloat("Horizontal", moveInput.x);
        animator.SetFloat("Vertical", moveInput.y);
        animator.SetFloat("Speed", moveInput.sqrMagnitude);

        if(animator.GetCurrentAnimatorStateInfo(0).IsName("Player_Attack_Left"))
        {
         moveSpeed = lungeDistance;
        } else {
         moveSpeed = moveSpeedDefault;
        }
    }


    void FixedUpdate() {
        
        moveInput.Normalize();
        rb2d.MovePosition(rb2d.position + moveInput * moveSpeed * Time.fixedDeltaTime);

    }




    private Vector2 GetPointerInput(){ ///bs for pointer
        Vector3 mousePos = pointerPosition.action.ReadValue<Vector2>();
        mousePos.z = Camera.main.nearClipPlane;
        return Camera.main.ScreenToWorldPoint(mousePos);


    }
}
#

tbh this is a big chunk of brain-spill so if too much effort i get that and i am continuing to look into this.

some clarity: i was previously not using
using unity.inputsystem;

teal sky
#

You may be no longer able to move due to your Animator. Try disabling it to test that

#

To clarify "ScreenToWorldPoint", "point" is not a verb here. It's a noun synonymous with "position"

#

It's really "convert a position on the screen to a position in the game world"