PJ/Assets/scripts/MOW.cs

81 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOW : MonoBehaviour
{
public GameObject GoTo;
private Vector2 TapPos;
public GameObject Player;
public int Speed = 10;
public Sprite PosTo;
public GameObject Screen;
public bool AllowMove;
public int MaxX;
public int MaxY;
public int MinX;
public int MinY;
private Rigidbody2D rb;
private Vector2 currentInput;
public Transform O;
public Transform I;
bool touched;
// Update is called once per frame
public void AMove()
{
if ( AllowMove)
{
TapPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (TapPos.y > MinY &&TapPos.y < MaxY && TapPos.x < MaxX && TapPos.x > MinX)
{
GoTo.transform.position = TapPos;
GoTo.GetComponent<SpriteRenderer>().sprite = PosTo;
}
}
}
//
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update() //eго на Update меняешь
{
currentInput = GatherInput();
}
private void FixedUpdate()
{
ApplyVelocity(currentInput);
}
public void SetTouched (bool touch)
{
touched = touch;
}
private Vector2 GatherInput()
{
//float x = -(O.position.x - I.position.x);
//float y = -(O.position.y - I.position.y);
//if(touched)
//return new Vector2(x, y).normalized;
//else return new Vector2(0,0);
//для стрелок раскоментируешь код ниже и замени "void Move" на "void Update"
//код выше наоборот закоментировать
float x = Input.GetAxisRaw("Horizontal");
float y = Input.GetAxisRaw("Vertical");
return new Vector2(x, y).normalized;
}
private void ApplyVelocity(Vector2 direction)
{
rb.linearVelocity = direction * Speed;
}
//
}