452892 (1) [Avatar] Offline
#1
Hi Mr. Hocking,

Just a couple of ?s

Question 1.
By the halfway of chapter 4 I have the simple FPS game running, with some walls, floors and a skybox functioning. My enemy respawns when killed, and will shoot a fireball at a time. However, the spawning fireball only seems to instantiate in front of the enemy box, and disappear, albeit registering the hit in my console. I'm unsure what's intended for the final result of ch.3., but I feel like I should be seeing said fireballs move towards me, so I have some hope of dodging. Turning down their float speed seems to not do anything.

Fireball script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fireball : MonoBehaviour
{
public float speed = 8.0f;
public int damage = 1;

private void Update()
{
transform.Translate(0, 0, speed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
PlayerCharacter player = other.GetComponent<PlayerCharacter>();
if (player != null)
{
Debug.Log("Player Hit");
}
Destroy(this.gameObject);
}
}


Question 2.
Although it does not prevent unity from compiling the final ch.3 game, with added wall / floor / skybox textures, I keep getting error:
550 - MissingComponentException - There is no CharacterController attached to the Main Camera game object, but a script is trying to access it.

Said script points to this in visual studio:

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

[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPSMoveInput")]
public class FPSMoveInput : MonoBehaviour
{
public float speed = 6.0f;
public float gravity = -9.0f;

private CharacterController _charController;

void Start()
{
_charController = GetComponent<CharacterController>();
}

void Update()
{
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);

movement.y = gravity;

movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.Move(movement);
}
}

Literally the last line _charController.Move(movement); seems to be causing Unity the grief.

Hope I havent asked anything stupid as my first post !

Many thanks for your book, thoroughly enjoying wrapping my head round it...a step at a time !
Chris
jhocking (169) [Avatar] Offline
#2
452892 wrote:Question 1.
By the halfway of chapter 4 I have the simple FPS game running, with some walls, floors and a skybox functioning. My enemy respawns when killed, and will shoot a fireball at a time. However, the spawning fireball only seems to instantiate in front of the enemy box, and disappear, albeit registering the hit in my console. I'm unsure what's intended for the final result of ch.3., but I feel like I should be seeing said fireballs move towards me, so I have some hope of dodging. Turning down their float speed seems to not do anything.


hm well you are correct that you should see the fireball flying through the air towards you. I would have assumed that it just was moving too fast to see, so are you sure you turned down the speed enough? Like, try a speed of 1 instead of 10 to see what happens; make sure to change the value in the Inspector and not the code, because public values set in the Inspector value overwrites what's in the code. Also, you might try watching the Scene view (ie click the Scene tab to switch from Game) while the game is running in order to watch from a better vantage point.

452892 wrote:Question 2.
Although it does not prevent unity from compiling the final ch.3 game, with added wall / floor / skybox textures, I keep getting error:
550 - MissingComponentException - There is no CharacterController attached to the Main Camera game object, but a script is trying to access it.


Well as the error suggests, the CharacterController component it's trying to use is missing. You might accidentally have an extra copy of the script attached to something in the scene; click on objects to see in the Inspector what script components are attached (as the error message implies, start by checking the camera).
107478 (2) [Avatar] Offline
#3
I ran into the same problem when working through this section today. I added some logging to the Fireball script at the beginning of OnTriggerEnter.
Debug.Log(string.Format("OnTriggerEnter {0}", other.ToString()));

I discovered that this function was being called immediately when the fireball was created because the fireball is colliding with the floor. Somewhere there is a step missing that sets the fireball's position high enough above the floor to avoid this collision.

I tried setting the Y position of the Fireball prefab to a higher value. This had no effect on the position of the fireballs that were created. (To make this obvious, I set the Y position to a value way over the player's head, like 5. The fireballs were still created at ground level.)

Then I tried transforming the fireball's position when it was first created. I changed this:
_fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f);

to this:
_fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f + Vector3.up * 0.25f);

This results in a fireball at eye level that does not intersect with the floor. Now the fireball moves until it hits the player or a wall, as expected.
jhocking (169) [Avatar] Offline
#4
Glad you figured out the issue you were having! I don't think that's the same problem as the original question (I'm increasingly suspecting the issue was changing the value in code and not the Inspector; I've done that myself) because of "albeit registering the hit in my console", although the word "hit" is open to interpretation. Anyway, the reason the code in the book doesn't raise the fireball higher is because the box enemy is actually positioned above the ground, in the middle of the box and not the bottom.
107478 (2) [Avatar] Offline
#5
I tracked down the discrepancy in my own project. In Figure 2.6 on page 26 you show the Position of the floor cube as (0, -0.5, 0). I missed that and had left it at its default value of (0,0,0).

This (just barely) resulted in a point of contact between all new Fireballs and the Floor at Y=0.5 unless I elevated the Instantiated Fireballs slightly.

If I correct my Floor's Y coordinate to -0.5, I no longer have to elevate my Fireballs.



466232 (1) [Avatar] Offline
#6
I had the same problem, walls and floor at (0,0,0). Surpringly however, the fireball seem to be higher than the floor when it sticked to the enemy. But still this is fixed with the vector3.up Thanks.