A For Excellent Work
Nice work with the spot light! That sure is one massive blast! Thanks for sharing ![]()
I Skimmed through the videos, and you don’t seem to be creating one complete games but instead teaching different parts of the game development? is that so? And then getting users to create their own projects using either their own assets or from the store? Or am I missing something
Watch the first video. That explains what you’ll be developing. I can’t teach you modelling or art but I’ll show you how to write the scripts that make up the FPS system.
Thanks! I did watch the first video. And a few of the others.
They all look great. Will be following along soon !
Video 51 Player Detect Item script for those who’d rather copy and paste it.
using UnityEngine;
using System.Collections;
namespace S3
{
public class Player_DetectItem : MonoBehaviour
{
[Tooltip("What layer is being used for items.")]
public LayerMask layerToDetect;
[Tooltip("What transform will the ray be fired from?")]
public Transform rayTransformPivot;
[Tooltip("The editor input button that will be used for picking up items.")]
public string buttonPickup;
private Transform itemAvailableForPickup;
private RaycastHit hit;
private float detectRange = 3;
private float detectRadius = 0.7f;
private bool itemInRange;
private float labelWidth = 200;
private float labelHeight = 50;
void Update()
{
CastRayForDetectingItems();
CheckForItemPickupAttempt();
}
void CastRayForDetectingItems()
{
if (Physics.SphereCast(rayTransformPivot.position, detectRadius, rayTransformPivot.forward, out hit, detectRange, layerToDetect))
{
itemAvailableForPickup = hit.transform;
itemInRange = true;
}
else
{
itemInRange = false;
}
}
void CheckForItemPickupAttempt()
{
if (Input.GetButtonDown(buttonPickup) && Time.timeScale > 0 && itemInRange && itemAvailableForPickup.root.tag != GameManager_References._playerTag)
{
//Debug.Log("Pickup attempted");
itemAvailableForPickup.GetComponent<Item_Master>().CallEvenPickupAction(rayTransformPivot);
}
}
void OnGUI()
{
if (itemInRange && itemAvailableForPickup != null)
{
GUI.Label(new Rect(Screen.width / 2 - labelWidth / 2, Screen.height / 2, labelWidth, labelHeight), itemAvailableForPickup.name);
}
}
}
}
I have a question why do I get this alerts in the Console?
link to image: Imgur: The magic of the Internet
LOL you wrote those warning messages to tell you if you forgot to type in the required data in the inspector for that script! If you double click the warning message it will take you to the place where you wrote it in the script
Can you help me I did your nav purse video and getting all kinds of errors. I know the tag one keeps popping up but I actually went into Game References and changed it there so that the enemy can find my player. I do not get why the other errors are happening the skeleton is not moving toward me.
Do not understand but deactivated reactivated the scripts errors went away but know the skeleton character keeps going into the ground when game is started his knees go into the floor. Can you help me? do you know whats wrong i got the errors gone but the skeleton keeps going into the ground why does it change position i baked all of the animation just confused. (I think i fixed errors but actually adding a path to it lol) so after messing around with it the animation is dropping the character but why and how do i fix it pls help?
I figured it out so the animations were making it go down in the ground so you go to the character and animations then you go to the loop settings and it has base upon start on one and u put on feet as start. It took a hour to figure that out by my self. no unity posts helped me . Just wanted to post because i would hate for someone else to go through this lol
using UnityEngine;
using System.Collections;
namespace S3
{
public class Enemy_Attack : MonoBehaviour {
private Enemy_Master enemyMaster;
private Transform attackTarget;
private Transform myTransform;
public float attackRate = 1;
private float nextAttack;
public float attackRange = 3.5f;
public int attackDamage = 10;
void OnEnable()
{
SetInitialReferences ();
enemyMaster.EventEnemyDie += DisableThis;
enemyMaster.EventEnemySetNavTarget += SetAttackTarget;
}
void OnDisable()
{
enemyMaster.EventEnemyDie -= DisableThis;
enemyMaster.EventEnemySetNavTarget -= SetAttackTarget;
}
// Update is called once per frame
void Update () {
TryToAttack ();
}
void SetInitialReferences()
{
enemyMaster = GetComponent<Enemy_Master> ();
myTransform = transform;
}
void SetAttackTarget(Transform targetTransform)
{
attackTarget = targetTransform;
}
void TryToAttack()
{
if (attackTarget != null) {
if (Time.time > nextAttack) {
nextAttack = Time.time + attackRate;
if (Vector3.Distance (myTransform.position, attackTarget.position) <= attackRange) {
Vector3 lookAtVector = new Vector3 (attackTarget.position.x, myTransform.position.y, attackTarget.position.z);
myTransform.LookAt (lookAtVector);
enemyMaster.CallEventEnemyAttack ();
enemyMaster.isOnRoute = false;
}
}
}
}
public void OnEnemyAttack()//called Swing normal
{
if (attackTarget != null) {
if (Vector3.Distance (myTransform.position, attackTarget.position) <= attackRange &&
attackTarget.GetComponent<Player_Master> () != null) {
Vector3 toOther = attackTarget.position - myTransform.position;
//Debug.Log (Vector3.Dot (toOther, myTransform.forward).ToString ());
if (Vector3.Dot (toOther,myTransform.forward) > 0.5F) {
attackTarget.GetComponent<Player_Master>().CallEventPlayerHealthDeduction(attackDamage);
}
}
}
}
void DisableThis()
{
this.enabled = false;
}
}
}
Does your OnEnemyAttack method ever get called?
Oh i got it to work for some reason it does not go through the whole animation and had to set event so it happened early but it is fine now. and can you do a scoring system like a kill counter. and and at end play screen u get to see your deaths and kills you got. it is probably simply but do not know how to code lol
Is it bad that i am having fun putting like a 100 skeletons in one room and see if i can survive. 400 is just overkill lol
Yeah it is pretty fun, especially when you can throw stuff at the enemies and hurt them. You could make different enemy types and make it interesting. In chapter 7 items will become much more interesting because you’ll be making exploding barrels as an example of what you can do.
I’ve made guys to you some recoil to the gun if you want
Please make a backup, so you don’t blame me if it doesn’t work, please …
Follow this to do it:
- Create a C# Script to the Gun Scripts Folder and Call it “Gun_Recoil”
- Add this code, it’s easy to understand :
using UnityEngine;
using System.Collections;
namespace SI
{
public class Gun_Recoil : MonoBehaviour {
private Gun_Master gunMaster;
#region Recoil Variables
private Vector3 currentRecoil1;
private Vector3 currentRecoil2;
private Vector3 currentRecoil3;
private Vector3 currentRecoil4;
public Vector3 recoilRotation;
public Vector3 recoilKickback;
#endregion
public Transform weaponHolder;
// Update is called once per frame
void FixedUpdate () {
RecoilController();
}
void OnEnable()
{
SetIntialReferences();
gunMaster.EventPlayerInput += AddRecoil;
}
void OnDisable()
{
gunMaster.EventPlayerInput -= AddRecoil;
}
void SetIntialReferences()
{
gunMaster= GetComponent<Gun_Master>();
}
void AddRecoil()
{
currentRecoil1 += new Vector3(recoilRotation.x, Random.Range(-recoilRotation.y, recoilRotation.y));
currentRecoil3 += new Vector3(Random.Range(-recoilKickback.x, recoilKickback.x), Random.Range(-recoilKickback.y, recoilKickback.y), recoilKickback.z);
}
void RecoilController()
{
currentRecoil1 = Vector3.Lerp(currentRecoil1, Vector3.zero,0.1f);
currentRecoil2 = Vector3.Lerp(currentRecoil2, currentRecoil1, 0.1f);
currentRecoil3 = Vector3.Lerp(currentRecoil3, Vector3.zero, 0.1f);
currentRecoil4 = Vector3.Lerp(currentRecoil4, currentRecoil3, 0.1f);
weaponHolder.localEulerAngles = currentRecoil2;
weaponHolder.localPosition = currentRecoil4;
}
}
}
- Make an Empty GameObject call it Holder and and it to the Assault Rifle
- Now, Go to Gun_Shoot Script and edit it to this …
using UnityEngine;
using System.Collections;
namespace SI
{
public class Gun_Shoot: MonoBehaviour {
private Gun_Master gunMaster;
private RaycastHit hit;
public float range = 400;
private Transform camTransform;
public Transform weaponHolder;
void OnEnable()
{
SetIntialReferences();
gunManager.EventPlayerInput += OpenFire;
//gunManager.EventSpeedCaptured += SetStartOfShootingPosition;
}
void OnDisable()
{
gunManager.EventPlayerInput -= OpenFire;
//gunManager.EventSpeedCaptured -= SetStartOfShootingPosition;
}
void SetIntialReferences()
{
gunMaster = GetComponent<Gun_Master>();
camTransform = transform.parent;
}
void OpenFire()
{
if (Physics.Raycast(camTransform.position + weaponHolder.localPosition, weaponHolder.forward, out hit, range))
{
gunManager.CallEventShotDefault(hit.point, hit.transform);
if(hit.transform.CompareTag(GameManager_References._enemyTag))
{
gunMaster.CallEventShotEnemy(hit.point, hit.transform);
}
}
}
}
}
- Don’t forgot to disable Gun_Animator Script …*
Thanks for a neat contribution there! I’ve added a link up above under the videos so that others can find your post in the future. Out of curiosity, are you still using the gun animator for the idle and shooting animations?
Oh i forgot that, you’ve to disable the Gun_Animator script for that ![]()
