- Home /
Question by
uncommonalmaz · Sep 09, 2019 at 04:04 PM ·
enemymobile devicesattackfollowingtaptomove
how to do to in second tap on screen of smartphone to create object and to this object look toward enemy but object look toward vector3 (0,0,0)
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);
}
}
Comment
Your answer
Follow this Question
Related Questions
how do I edit enemy stats 3 Answers
Damaging enemies... 1 Answer
Get First Object In List 2 Answers
the attack is synchronized if there is more than one enemy 1 Answer
Enemy follows player then attacks! 2 Answers