Home > other >  Unity2d game characters keyboard control to the mouse click control, with the corresponding animatio
Unity2d game characters keyboard control to the mouse click control, with the corresponding animatio

Time:09-28

The role of mobile application, use the keyboard aswd control up and down or so, I want to change to use the mouse control, the mouse to click position, role can automatically go past (not teleport), and then with the corresponding animation have bosses can you help me to change my program, animation with a mixture of
Public class PlayerMovement: MonoBehaviour
{
Rigidbody2D rb.
Collider2D coll.
Animator anim.

Public float speed;
Vector2 movement;

Private void Awake ()
{
Rb=GetComponentColl=GetComponentAnim=GetComponent (a);
}

Private void the Update ()
{
The Movement ();
SwitchAnim ();
}

Void Movement ()//move
{
Movement. X=Input. GetAxisRaw (" Horizontal ");
Movement. Y=Input. GetAxisRaw (" Vertical ");
Rb. MovePosition (rb. Position + speed movement * * Time. DeltaTime);

}

Void SwitchAnim ()/switch/animation
{
If (movement!=Vector2. Zero)//that Horizontal to 0, keeping movment values to switch the idle animation blend tree
{
Anim. SetFloat (" horizontal ", movement. X);
Anim. SetFloat (" vertical ", movement. Y);
}
Anim. SetFloat (" speed ", the movement magnitude);//magnitude can also use sqrMagnitude specific reference Api can be the default return value forever & gt;=0
}
}

CodePudding user response:

Can be done with ray

CodePudding user response:

1. To get a plane can click
2. To determine whether a state of control can click in the update status

String m_strClickArea_Tag="ClickArea";
If (m_isChoiceGameObject)//clickable state
{
If (Input. GetMouseButtonDown (0))
{
M_isChoiceGameObject=false;//points after the reset to not click
Vector3 touchPos=Input. MousePosition;
Camera curCamera=m_mainCamera;
If (curCamera!=null)
{
TouchPos. Z=curCamera. NearClipPlane;
TouchPos=curCamera. ScreenToWorldPoint (touchPos);
Vector3 dir=touchPos - curCamera. The transform. The position;
RaycastHit [] hits=Physics. RaycastAll (curCamera. The transform. The position, dir);
For (int I=0; I & lt; Hits. Length; I++)
{
RaycastHit hit=hits [I];
If (hit.transform.Com pareTag (m_strClickArea_Tag))
{
//if (EventSystem. Current. IsPointerOverGameObject ())
//{
//return;
//}
//the world coordinates,
Vector3 Vector3=new Vector3 (hit) point) x, 0, hit the point. The z);
//screen coordinates
Vector3 vector3Screen=curCamera. WorldToScreenPoint (Vector3);
  • Related