I would like some advice on the operations (moving) that Unity2d would like to implement.

Asked 2 years ago, Updated 2 years ago, 460 views

I am creating omnidirectional shooting with Unity2d.
I would like to implement WASD movement with the cursor position of the mouse as the front.
  

WASD movement on the XY axis, between the actual machine (player) position and the mouse position, etc. I only know how to move easily.

I can't figure out how to move the WASD on the mouse position axis, and I can't think of a way.
It might be easier to understand if you say WASD movement of the point of view (tilt) axis.
To supplement the image,

Use the W key to move forward in the direction you are facing (mouse position).
It feels like you can move just by using the mouse while pressing the W key.
Move back and forth in Vector 3.MoveTowards,
I tried my best to add left and right movements, but I couldn't.

In the first place, I was wondering if it's better not to use Vector when using this method of movement. I don't even know that other things like CharacterController and RigidBody are better.

There are easy-to-understand planes that attack the mouse pointer with WASD operation. I wanted to add a slightly unusual aircraft, but it seems difficult.

Please give me some advice even if it's similar.

c# unity2d

2022-09-30 22:04

1 Answers

It's easy to make, but I think it's hard to operate.

using UnityEngine;

RequireComponent (type of (Rigidbody2D))]
public class movement —MonoBehavior
{
    SerializeField float_speed=3f;
    Rigidbody2D_rb = default;

    void Start()
    {
        _rb = GetComponent <Rigidbody2D>();
        _rb.gravityScale=0;
        _rb.freezeRotation=true;
    }

    void Update()
    {
        Vector3lookAtPosition=Camera.main.ScreenToWorldPoint (Input.mouthPosition);
        lookAtPosition.z = transform.position.z;
        transform.up =lookAtPosition-transform.position;
        float=Input.GetAxisRaw("Horizontal");
        float v=Input.GetAxisRaw("Vertical");
        _rb.velocity=transform.TransformVector(new Vector3(h,v,0).normalized)*_speed;
    }
}


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.