I have problems with Enemy and Player scripts.
4 part question. Yes I am new to all this. No I am not asking someone to build the whole thing for me. And I do have learning disabilities so i need each line where corrections can be made specified. Note that I'm using Unity 5.2.1.
1:
How do i stop enemy target blowing up on contact with terrain, because it also destroys terrain. script:
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
void Start()
{
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
Destroy(other.gameObject);
Destroy(gameObject);
}
}
2:
Enemies are not spawning in different spawn locations with the spawn points already placed where they should. They spawn at the same point. I try and remove parts of this script and i get errors in visual studio. When they do spawn its under the terrain.
script.
public class EnemySpawner : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public float spawnTime = 4f;
public Transform[] spawnPoints;
public float waveWait;
public object spawn
{
get; private set;
}
void Start()
{
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation); Instantiate(hazard, spawnPosition, spawnRotation);
}
}
My character for player is a flying character. The Enemy AI_Follow causes the enemies to float up off the ground even with rigid body check for using gravity and kinematics, and fix to the Y and X,Y,Z axes.
As an extra note my playing character also has a rigid body and collider but still passes through the ground.
Side note:
haven't been abler to figure out how to apply health properly to enemies or character which obviously i need in place before attempting to script a Game Controller or GUI.
The four Player Scripts are as follows;
The first two are for the shooting.
Movement for Bullet (has a destroy its clone option):
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed =10.0f;
void Start ()
{
GetComponent<Rigidbody>().velocity = transform.forward * speed;
Destroy(this.gameObject, 6);
}
}
Shoot Script (where the bullet comes out):
using UnityEngine;
using System.Collections;
using System;
public class Shoot : MonoBehaviour
{
public GameObject bullet;
public float delayTime = 1;
private float counter = 0;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse0) && counter > delayTime)
{
Instantiate(bullet, transform.position, transform.rotation);
GetComponent<AudioSource>().Play();
counter = 0;
}
counter += Time.deltaTime;
}
internal static void DisableEffects()
{
throw new NotImplementedException();
}
}
(I know how to add to have two different types of enemies in this script, but its pointless if they spawn in the same location and blow each other and the terrain up.
The second two are player movement
Allows me to look and move in that direction.
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
public float LookSensitivity = 0.0f;
public float xRotation;
public float yRotation;
public float currentXRotation;
public float currentYRotation;
public float xRotationV;
public float yRotationV;
public float lookSmoothDamp = 0.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
yRotation += (Input.GetAxis("Mouse X") * LookSensitivity);
xRotation -= (Input.GetAxis("Mouse Y") * LookSensitivity);
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}
}
Basic movement Forward; Backward and Side to side but still facing on the Z axis hence the reason for using the mouse look. (PS changing these codes throw me constant errors)
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 45.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
}
}
So at this point i'm really stuck and i haven't seen any tutorial videos that help with C# specific Unity 5+ and many of the references i have searched through are badly out of date.
sorry the second script is smooshed. I'll try again.
using UnityEngine; using System.Collections;
public class EnemySpawner : $$anonymous$$onoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public float spawnTime = 4f;
public Transform[] spawnPoints;
public float waveWait;
public object spawn
{
get; private set;
}
void Start()
{
InvokeRepeating("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation); Instantiate(hazard, spawnPosition, spawnRotation);
}
}
Ill be back because im trying to figure out how to get the enemy snowman to shoot back