本文主要是介绍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;
}
}
这篇关于unity2d角色跟随鼠标移动的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!