Player Mouse

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour


{
//variaveis do Player
public float velocidade;
private Rigidbody2D rig;
private Vector2 movimento;
//seguir o Mouse
public Camera cameraDoJogo;
private Vector2 posicaoDoMouse;
//criar a referencia da bola
public GameObject bola;
public Transform disparo;

public int vidasMax;


public int vidasAtual;
// Start is called before the first frame update
void Start()
{
rig=GetComponent<Rigidbody2D>();
vidasAtual=vidasMax;
}

// Update is called once per frame


void Update()
{
//movimento pelo teclado
movimento.x=Input.GetAxisRaw("Horizontal");
movimento.y=Input.GetAxisRaw("Vertical");
rig.MovePosition(rig.position + movimento * velocidade);
//seguir mouse
posicaoDoMouse=cameraDoJogo.ScreenToWorldPoint(Input.mousePosition);
Vector2 direcaoDoOlhar = posicaoDoMouse - rig.position;
float anguloDoJogador=Mathf.Atan2(direcaoDoOlhar.y,direcaoDoOlhar.x) *
Mathf.Rad2Deg;
rig.rotation=anguloDoJogador;

Disparar();

}
void Disparar(){
if(Input.GetButtonDown("Fire1")){
Instantiate(bola,disparo.position,disparo.rotation);

}
}
public void acertou(int dano){
vidasAtual -= dano;
if (vidasAtual <=0 ){
Destroy(this.gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Alvo : MonoBehaviour


{
public float velocMinima;
public float velocMaxima;
public float velocAtual;

private Rigidbody2D rig;


private Transform acharPlayer;

public int vidasMax;


public int vidasAtual;
public int dano;
// Start is called before the first frame update
void Start()
{
rig=GetComponent<Rigidbody2D>();
velocAtual=Random.Range(velocMinima,velocMaxima);
acharPlayer = FindAnyObjectByType<Player>().transform;
velocAtual=vidasMax;
}

// Update is called once per frame


void Update()
{
mover();

}
void mover(){

transform.position=Vector2.MoveTowards(transform.position,acharPlayer.position,
velocAtual * Time.deltaTime);
}
public void acertou(int dano){
vidasAtual -= dano;
if (vidasAtual <=0 ){
Destroy(this.gameObject);

}
}
void OnCollisionEnter2D(Collision2D colidir){
if(colidir.gameObject.CompareTag("Player")){
colidir.gameObject.GetComponent<Player>().acertou(dano);
}
}
}

You might also like