- Home /
I need help scripting a shooter
I'm quite new to programming, my teacher asked us to make a shooter with a few limitations, like the enemies can only attack every 5 seconds and such. I'm nearly done, however unity tells me I've made at least 11 mistakes, here's the code of the MainCharacter and Enemy scripts:
using UnityEngine; using System.Collections;
public class Enemy : MonoBehaviour { void Start () {
}
public float Velocidad = 10;
private bool Atacar = false;
RaycastHit RCH;
public float DistanciaParaAtacar = 20;
void Update ()
{
transform.LookAt(MainCharacter.Jugador.position);
transform.position += transform.forward * Time.deltaTime * Velocidad;
if (Vector3.Distance(this.transform.position, MainCharacter.Jugador.position) < DistanciaParaAtacar)
{
//Atacar
}
transform.LookAt(MainCharacter.Jugador.position);
if (Physics.Raycast(transform.position,transform.forward, out RCH, Mathf.Infinity))
{
if (RCH.collider.gameObject.layer == MainCharacter.LAYER_MAIN_CHARACTER)
{
MainCharacter mc = (MainCharacter)RCH.collider.GameObject.GetComponent(typeof(MainCharacter));
mc.TakeDamage(20);
}
}
}
public float Vida = 200;
public void TakeDamage(float damage)
{
Vida -= damage;
if (Vida <= 0)
{
GameObject.Destroy(this.gameObject);
}
}
}
using UnityEngine; using System.Collections;
public class MainCharacter : MonoBehaviour { public GameObject Jugador; void Start () {
}
RaycastHit RCH;
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(Camera.mainCamera.transform.position, Camera.mainCamera.transform.forward, out RCH, Mathf.Infinity))
{
if (RCH.collider.GameObject.layer == LAYER_ENEMY)
{
Enemy e = (Enemy)RCH.collider.GameObject.GetComponent(typeof(Enemy));
if (e)
{
e.TakeDamage(50);
}
}
}
}
}
public float Vida = 200;
public void TakeDamage(float damage)
{
Vida -= damage;
if (Vida <= 0)
{
Application.LoadLevel(0);
}
}
}
What it should do is allow both my player and my enemies to attack, and when they hit each other using a raycast, they should substract from the var Vida, also when Vida is 0 in enemies, they should be destroyed, and in the player, the whole proyect should reload. However the error log basically says that theres problems with the MainCharacter.Jugador.Transform
If someone could help me I'd be very grateful. Please answer as fast as you can.
what does it do? what should it do?
and please format the rest of your code.
Anyone else notice how the school assignment questions always seem like the $$anonymous$$cher's aren't $$anonymous$$ching anything, rather just saying "Hey, make it do this kinda stuff." and then going back to the coffee room... ? You'd think since these things are school assignments the $$anonymous$$cher's would have taught (or be $$anonymous$$ching) how these things are done, ins$$anonymous$$d of throwing a computer at the student and sending them to this answer board.
I'm beginning to feel like I'm back in high school looking at all of these projects ;)
Now that was a long time ago... Not sure Unity would run on a Radio Shack TRS80 III
Just looking over the code, I see a bunch of undefined variables, including the one you pointed out in your question. It looks like you are also trying to use an entire class as an instance as well. Check that and post up the rest of the errors.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071907im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Vertical Shooter Joystick Setup 0 Answers
projectile to cursor for a side scrolling shooter 0 Answers
My Enemy AI doesn't work(FPS). 1 Answer
Unity FPS Tutorial delted?! 3 Answers
Error in code I cannot fix 1 Answer