unity2d角色跟随鼠标移动

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

public class PlayerMovement : MonoBehaviour
{
    public float speed;//速度
    new private Rigidbody2D rigidbody;//刚体
    private Animator animator;//动画
    private float inputX, inputY;//输入
    private float stopX, stopY;
    public Joystick joystick;//遥感
    private Vector2 mousePos;//鼠标位置
    private Vector2 direction;//跟随
    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        
    }

    void Update()
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);//鼠标位置转换
       // Shoot();
        inputX = joystick.Horizontal;
        inputY = joystick.Vertical;
        Vector2 input = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized;//鼠标位置旋转跟随//(transform.right * inputX + transform.up * inputY).normalized;
        rigidbody.velocity = input * speed;

        //if (input != Vector2.zero)
        //{
        //    animator.SetBool("isMoving", true);
        //    stopX = inputX;
        //    stopY = inputY;
        //}
        //else
        //{
        //    animator.SetBool("isMoving", false);
        //}
        //animator.SetFloat("InputX", stopX);
        //animator.SetFloat("InputY", stopY);

    }
    public void Shoot()
    {
        direction = (mousePos - new Vector2(transform.position.x, transform.position.y)).normalized ;
        transform.right = direction;

    }
}

你可能感兴趣的:(动画,unity,游戏引擎)