Hi,
I am currently coding a game which works like the puck things you find in arcades.
The problem I am havign is that the boolean sometimes registers isMoving and other times does not when the spacebar is pressed.
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private GameObject tempHitVar;
public PhysicMaterial PuckPhysicsMaterial;
// Start is called before the first frame update
void Start()
{
tempHitVar = GameObject.FindGameObjectWithTag("PlayerPuck");
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(0))
{
//Shoot a ray onto the scene at the mouse postiion.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Did we hit anything???
if(Physics.Raycast(ray, out RaycastHit hit))
{
//If we hit the player puck.
if(hit.collider.tag == "PlayerPuck")
{
//The player has "picked up" the puck
bool isSelected = tempHitVar.GetComponent<PuckController>().isSelected = true;
//Set all the box collides to null physics material as while the player is "holding" the puck we dont want it to "bounce" upon dropping it.
BoxCollider[] bc = tempHitVar.GetComponents<BoxCollider>();
foreach(BoxCollider boxCol in bc)
{
boxCol.material = null;
}
}
}
//#TODO : Update the location on the X and Z axis allowing them to drop it in a different location to start the game.
}
Rigidbody rb = tempHitVar.transform.gameObject.GetComponent<Rigidbody>();
if(Input.GetMouseButtonUp(0))
{
bool unSelected = tempHitVar.gameObject.GetComponent<PuckController>().isSelected = false;
}
//If the player has pressed the space button and is not currently "holding" the puck.
if(Input.GetKeyDown(KeyCode.Space) && (tempHitVar.gameObject.GetComponent<PuckController>().isSelected == false))
{
//If the puck is moving, we must wait for it to finish.
if(tempHitVar.gameObject.GetComponent<PuckController>().isMoving == true)
{
Debug.Log("Puck is moving cannot hit again yet....");
}else{
//Add a specified amount of "push" force to the puck.
rb.AddForce(Vector3.forward * tempHitVar.gameObject.GetComponent<PuckController>().PushSpeed);
// The puck is now moving...
tempHitVar.GetComponent<PuckController>().isMoving = true;
//Update the box colliders on the puck (I am using 2 box colliders in a cross shape pattern to detect collisions,
//There was no cylinder collider and i could not use Gravity with a Mesh Collider.)
BoxCollider[] bc = tempHitVar.GetComponents<BoxCollider>();
foreach(BoxCollider boxCol in bc)
{
boxCol.material = PuckPhysicsMaterial;
}
}
}
//if the puck is moving.
if(tempHitVar.gameObject.GetComponent<PuckController>().isMoving == true)
{
//Disable the aiming tool
Transform Aimer = tempHitVar.transform.Find("Aimer");
Aimer.gameObject.SetActive(false);
//Once the puck has stopped re-enaable the aiming tool to allow the player to aim the firing direction.
}else if(tempHitVar.gameObject.GetComponent<PuckController>().isMoving == false){
Transform Aimer = tempHitVar.transform.Find("Aimer");
Aimer.gameObject.SetActive(true);
}
//RESET THE PUCKS POSITION AND ROTATION. <<DEMO FUNCTION.
if(Input.GetKeyDown(KeyCode.B))
{
if(rb.velocity != Vector3.zero)
{
tempHitVar.gameObject.GetComponent<PuckController>().isMoving = false;
rb.velocity = new Vector3(0,0,0);
tempHitVar.gameObject.transform.position = new Vector3(0f, 0.05f,-3f);
Quaternion myRotation = Quaternion.identity;
myRotation.eulerAngles = new Vector3(0,0,0);
Debug.Log("RESETTING...");
}
}
//This controls the "aimer" and allows it to rotate around the position of the puck.
if(Input.GetKey(KeyCode.N))
{
SpriteRenderer sr = tempHitVar.gameObject.GetComponentInChildren<SpriteRenderer>();
sr.transform.RotateAround(tempHitVar.transform.position,Vector3.up, -tempHitVar.gameObject.GetComponent<PuckController>().turnSpeed * Time.deltaTime);
//# TO DO : Send the current rotation of the aimer to the puck before firing so we can fire the puck in that direction.
}
if(Input.GetKey(KeyCode.M))
{
SpriteRenderer sr = tempHitVar.gameObject.GetComponentInChildren<SpriteRenderer>();
sr.transform.RotateAround(tempHitVar.transform.position,Vector3.up, tempHitVar.gameObject.GetComponent<PuckController>().turnSpeed * Time.deltaTime);
}
}
}
Then I have this on the actual puck.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuckController : MonoBehaviour
{
public float HoldHeight;
public bool isSelected;
public float PushSpeed;
public bool isMoving;
public float turnSpeed;
// Update is called once per frame
void Update()
{
if(isSelected)
{
transform.position = new Vector3(transform.position.x,HoldHeight, transform.position.z);
}
Rigidbody rb = transform.GetComponent<Rigidbody>();
if(rb.velocity == Vector3.zero)
{
isMoving = false;
}
}
}
If anyone can tell me why the isMoving sometimes triggers and other times it doesn,t, it would be apprecuated!
Many thanks!
