简体   繁体   English

让 2d 播放器查看 cursor,然后朝它移动

[英]Making a 2d player look at the cursor, and then move towards it

This is the code:这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    // Tweekable move speed
    public float MoveSpeed = 4;

    // Set what distance it should be from the target
    int MaxDist = 10;
    int MinDist = 5;

    // This is the target
    public Transform Mouse;


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

    // Update is called once per frame
    void Update()
    {
        // Keep looking at an object
        transform.LookAt();

     // Turn off gravity
    Physics2D.gravity = Vector2.zero;

    // When distance is over minimun
    if (Vector3.Distance(Mouse.position, Mouse.position) >= MinDist)
    {
        // Keep distance
        transform.position += transform.forward * MoveSpeed * Time.deltaTime;

        // When it's between max and minimun
        if (Vector3.Distance(transform.position, Mouse.position) <= MaxDist)
        {


        }

    }
}

// I imported this code from another project so the enemy keeps looking at the player, and keeps his distance. // 我从另一个项目中导入了这段代码,所以敌人会一直看着玩家,并保持距离。 it was a 3D project, so there may be more errors, and this is also my first time creating a game.这是一个3D项目,所以可能会有更多错误,这也是我第一次创建游戏。 Be gentle要温柔

I am working on a similar project right now.我现在正在做一个类似的项目。 Here is the code I use to rotate the player (first part) and then move it into that direction (second part):这是我用来旋转播放器的代码(第一部分),然后将其移动到该方向(第二部分):

public Rigidbody2D rb;
public float speed;

void Update()
{ 
   //get mouse position
   Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   //calculating the angle
   float AngleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - 
   transform.position.x);
   float AngleDeg = (180 / Mathf.PI) * AngleRad;
   //rotating the player
   Player.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

   //second part
   Vector3 mouseDir = mousePos - transform.position;
   mouseDir.z = 0f;
   mouseDir = mouseDir.normalized;
   rb.AddForce(mouseDir * speed);
}

If the player rotates, but it is looking in the wrong way (in my case), then play around with the the last line of the first part.如果播放器旋转了,但它以错误的方式看(在我的情况下),然后玩第一部分的最后一行。 The following code worked fine for me:以下代码对我来说很好:

transform.rotation = Quaternion.Euler(0, 0, AngleDeg - 90);

If you want the player to move at a constant speed, then you can use如果您希望玩家以恒定速度移动,那么您可以使用

rb.velocity = mouseDir * speed;

instead of代替

rb.AddForce(mouseDir * speed);

in the last line of the second part.在第二部分的最后一行。

Your Player-Gameobject needs a Rigidbody2D component.您的 Player-Gameobject 需要一个 Rigidbody2D 组件。 I also added an if-statement around the second part, so the player only moves to the wanted direction when (for example) space is pressed, but always looks to the mouse.我还在第二部分周围添加了一个 if 语句,因此玩家只有在(例如)按下空格时才会移动到想要的方向,但总是看向鼠标。

philale菲莱尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM