this is my joystick
public class myJoystick : MonoBehaviour
{
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public float time2attack;
public float time;
public GameObject tentacle;
private bool Cattack = false;
// Start is called before the first frame update
void Start()
{
time = time2attack;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
if (Input.GetMouseButton(0))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
attack();
}
private void FixedUpdate()
{
if (touchStart)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction * 1);
}
}
void moveCharacter(Vector2 direction)
{
player.Translate(direction * speed * Time.deltaTime);
time -= Time.deltaTime;
if (time <= 0)
{
time = time2attack;
}
}
void attack()
{
if (time == 0)
{
time = time2attack;
}
}
}
this is code which make to object look toward enemy
public class attackTentacle : MonoBehaviour
{
public Transform target;
private float speed = 100f;
// Update is called once per frame
void Update()
{
Vector3 vectorToTarget = target.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
}
}
↧