M
M
mari_greys2019-08-12 16:04:03
2D
mari_greys, 2019-08-12 16:04:03

Need help with character control in 2d, can you help?

It is necessary that the character move with the help of a mouse click in a 2D game, I already have a script for 3D. I
could not find an adequate lesson, and the documentation is also a little blind, please help, it will be very useful.
I know that you need to send rays through the vector, but it’s still difficult with the code on the sharpe. The character also has Rigidbody2D physics.
If there is something else you need, just tell

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Filonenko, 2019-08-12
@kombain32

I scared you and you decided to open a new question?))
Let's try again. Tell me something works for you? That is, you click on the launch of the program, and what happens next (is it compiled?)? What can you do on stage? What objects are on the stage? What is a character? (that is, it is apparently some kind of object on which control scripts hang correctly?) Let's take screenshots of its inspector. Show us at least something that you have written, have any objects been made? (Tell us what you do to create something, the inhabitants of the Toaster will probably be able to tell you how to start doing something right). Send screenshots of the code, object inspectors - this is such a thing https://docs.unity3d.com/Manual/UsingTheInspector , just screenshots from your game.
Where did you get the code you're talking about? Probably it is difficult for you and you should start with something simpler (tetris or tag). The most correct way to start reading is here https://docs.unity3d.com/Manual/
UPD: "I know that you need to send rays through the vector, but it's still difficult with the code on the sharpe" - you probably read this somewhere or you are bad people it was whispered, but I have not heard such a definition of RayCast (a) (you'd better forget and it's not at all clear why use it for movement). And without C#, nowhere in Unity (absolutely), so study the literature on Sharpik.
UPD2: This forum is specifically designed to ask questions and get answers from the community. Nobody eats you here, so get in touch and do not produce the same questions. 90% of developers started like you.

A
Ark Tarusov, 2019-08-18
@kreo_OL

The most elementary way

[SerializeField]private GameObject player;
        [SerializeField] private float speed=2f;
        private Camera camera => Camera.main;
        private bool isMove;
        private Vector3 targetPosition;
        private void Update()
        {
            if (Input.GetMouseButton(0))
            {
                targetPosition = camera.ScreenToWorldPoint(Input.mousePosition);
                targetPosition.z = 0;
                isMove = true;
            }

            if (isMove)
            {
                player.transform.position =
                    Vector3.Lerp(player.transform.position, targetPosition, Time.deltaTime * speed);
                if ((targetPosition-player.transform.position).sqrMagnitude < 0.1f)
                {
                    isMove = false;
                }
            }
        }

If you need something more complicated, you can, in principle, directly apply.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question