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;
}
}
标签:mousePos,鼠标,角色,Vector2,unity2d,transform,private,public,animator 来源: https://blog.csdn.net/meng132512/article/details/122693805