I am developing a 2D game in unity. One of the features in the game is to shoot projectiles using the left click of the mouse. Upon releasing the left click the projectile gets fired with certain amount of force that depends on how long the player held the left click.
The problem is that sometimes when I release the left click the game doesnt seem to detect it and the release portion of the code doesnt get executed until I click and release again. I may not sound like a big problem but input reliability will play a fundamental role in this game.
So, is there any way to make mouse input more realiable? I have already tried using Input.GetMouseButtonDown and different kinds of conditionals in order to make it more reliable, but it hasnt worked out. Thank you in advance!
Here is the code I am using:
using UnityEngine;
using System.Collections;
public class ShootPlasma : MonoBehaviour {
//prefab
public Transform bulletPrefab;
//Reloading variables:
public int numberOfBullets;
private int numberOfBulletsRecord;
private bool canShoot=true;
public float timeToReload;
private float timeToReloadRecord;
//direction and bullet Speed variables:
Transform sightPosition;
public Vector3 SpawnRiseVector;
private Vector2 direction;
public float bulletBoost;
private float bulletBoostRecord;
public float MAX_BOOST;
//Arrow Guide
public Transform aimingArrow;
//Helper variables;
private CharacterController2D charControllerScript;
void Start () {
timeToReloadRecord = timeToReload;
numberOfBulletsRecord = numberOfBullets;
charControllerScript = transform.parent.GetComponent<CharacterController2D> ();
bulletBoostRecord = bulletBoost;
sightPosition = GetComponent<Transform> ();
aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
}
// Update is called once per frame
void FixedUpdate () {
if(numberOfBullets<=0){
canShoot=false;
if(!canShoot){
timeToReload-=Time.deltaTime;
//if the waiting time has ended restart variables.
if(timeToReload<=0.0f){
canShoot=true;
timeToReload=timeToReloadRecord;
numberOfBullets=numberOfBulletsRecord;
}
}
}
////////////////////////////////// SHOOTING CODE ////////////////////////////////////////////
///
/// MOUSE DOWN
/////////////////////////////////////////////////////////////////////////////////////////
else if(Input.GetMouseButton(0)&& canShoot && !Input.GetMouseButtonUp(0)){
//show the Arrow Guide:
if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=true){
aimingArrow.GetComponentInChildren<Renderer>().enabled=true;
}
//calculate the distance between the mouse and the sight;
Vector3 mousePositionRelative=Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction= new Vector2(mousePositionRelative.x- sightPosition.position.x,
mousePositionRelative.y- sightPosition.position.y).normalized;
//If Y is less or equal 0:
if(direction.y<=0.0f && direction.x>=0.0f){
direction=new Vector2(1.0f,0.0f);
}
else if(direction.y<=0.0f && direction.x<0.0f){
direction=new Vector2(-1.0f,0.0f);
}
//Rotate the aiming arrow
if(charControllerScript.facingFront){
if(direction.x>=0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
else if(direction.x<0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
}
else if(!charControllerScript.facingFront){
if(direction.x>=0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
else if(direction.x<0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
}
Debug.Log(direction);
//Charge
bulletBoost+=Time.deltaTime*bulletBoost;
if(bulletBoost>=MAX_BOOST){
bulletBoost=MAX_BOOST;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// MOUSE UP
/// ///////////////////////////////////////////////////////////////////////////////////////////////
else if(Input.GetMouseButtonUp(0)&& canShoot && !Input.GetMouseButton(0)){
//Hide the Arrow Guide:
if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=false){
aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
}
//Fire
var shootPrefab= Instantiate(bulletPrefab,sightPosition.position+SpawnRiseVector,Quaternion.identity) as Transform;
shootPrefab.GetComponent<Rigidbody2D>().AddForce(direction*bulletBoost);
bulletBoost=bulletBoostRecord;
//Reduce the Ammo by one:
numberOfBullets-=1;
}
}
}
The problem is that you are using FixedUpdate() instead of Update(). FixedUpdate() is called in constant intervals(not necessary each frame). So, the Input.GetMouseButtonUp() may be missed some time between 2 calls of FixedUpdate(). You should better use Update() when you are handling input.
Related
I'm developing a 2D mobile game where the player should tap on the screen to go up. I just added a pause button recently and I've noticed that when I click the button it counts as a tap on the screen, so the player goes up. Basically, in the same exact moment that I press the pause button, the player jumps and then the game freezes while he is in mid-air. Is there a way I could make so the button wouldn't count as a tap?
Here's the part of the player script that makes him move:
if (Input.GetMouseButtonDown (0) && isDead == false && isKnifeDead == false && UiStartScript.uiInstance.firstClickUi == true) {
rb.velocity = Vector2.up * velocity;
}
Heres's the part of the UI script for the pause button:
public void PauseGame()
{
Time.timeScale = 0;
}
My EventSystem:
EventSystem
My Button:
Button
An easy with no code required is to remove the code about detecting tap on screen and instead place a button that covers the whole screen (invisible button).
That button event would trigger the screen movement.
You can add extra buttons on top and Unity will automatically discard the tap from any other button below.
This way you can start adding UI all over without checking in code which should be considered.
In U. I script add
public GameObject PlayerConrtollerScript;
In your U.I Script, under the class Pausgame()
Add this code.
GameObject.PlayerControllerScript.SetActive = false;
When unpaused you need to add to your script that has the
Time. timeScale =1;
Add this code.
GameObject.PlayerControllerScript.SetActive = true;
After you have added the code mentioned above. Don't forget to add the player game object from your hierarchy into the Inspector with the U. I script, the space will be available to add a game object when you scroll down to your U. I script.
That will stop your player from moving around during pause.
This is the approach that I been done. I create a cople of tricks using Event System and Canvas for take advantage of the Mouse Input, you don't need the use of Updates functions for do the basic gameplay.
Here's my example project: TapAndJump on Github
Check the GameObjects called "Click Panel" and "Pause Button" and
respective linked methods.
JumpWithTap.cs
using UnityEngine;
public class JumpWithTap : MonoBehaviour
{
[SerializeField] private UIHandler uiHandler;
[SerializeField] private float jumpPower = 5f;
private Rigidbody2D rb2D;
private int counter;
#region Class Logic
public void Jump()
{
rb2D.velocity = Vector2.up * jumpPower;
uiHandler.SetNumberToCounter(++counter);
}
#endregion
#region MonoBehaviour API
private void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
}
#endregion
}
UIHandler.cs
using UnityEngine;
using UnityEngine.UI;
public class UIHandler : MonoBehaviour
{
[SerializeField] private Text counterText;
[SerializeField] private Text pauseText;
[SerializeField] private Image clickPanel;
private bool isPaused = false;
public bool IsPaused
{
get { return isPaused; }
set
{
pauseText.gameObject.SetActive(value);
isPaused = value;
}
}
#region Class Logic
public void SetNumberToCounter(int number) => counterText.text = number.ToString();
public void DisableClickPanel() => clickPanel.raycastTarget = !clickPanel.raycastTarget;
public void PauseGame()
{
IsPaused = !IsPaused;
DisableClickPanel();
// Time.timeScale = IsPaused == true ? 0 : 1; // If you want keep the Physics (ball fall) don't stop the time.
}
#endregion
}
I guided by your code example and so on; be free that modify and request any issue.
These past month+ I learned many things by making a game in Unity.I have a lot of fun doing so. But some thing are still confusing me. I'm trying to setup a skill to the character and it goes almost well. When the character is casting the skill, the skill goes behind the character and not in front. So i thought to play with positions and rotations to make it work but still nothing. Worth to mention that the prefab has it's own motion. So my code so far is this. So help would be great and some teaching about the logic behind the skills system would be much appreciated. So take a look:
using UnityEngine;
public class MagicSkill : MonoBehaviour
{
public GameObject hand; // Players right hand
public GameObject fireballPrefab; // Fireball particle
Vector3 fireballPos; // Adding the transform.position from the players hand
Quaternion fireballRot; // Adding the rotation of the players hand
private bool isPressed = false; //Skill button (mobile)
public Animator animator; // Casting spell animation
void Update()
{
fireballPos = hand.transform.position; // Getting the position of the hand for Instatiating
fireballRot.x = hand.transform.rotation.x; // Getting the rotation of the hand for x Axis
fireballRot.y = hand.transform.rotation.y; // Getting the rotation of the hand for y Axis (Here i try to modify the values but still nothing)
fireballRot.z = hand.transform.rotation.z; // Getting the rotation of the hand for z Axis
if (isPressed == true)
{
animator.SetBool("Magic", true);
if (hand.transform.position.y >= 20) // Here I got the exact position of the hand for when to
Instatiate the skill
{
for (int i = 0; i < 1; i++) // For some reason it instatiates too many prefabs (I think it's because it's in Update method)
{
Instantiate(fireballPrefab, fireballPos, Quaternion.Euler(fireballRot.x, fireballRot.y, fireballRot.z));
Invoke("Update", 2.0f); // I'm trying to slow down the pressed button so that it want spawn every frame
}
}
}
else
{
animator.SetBool("Magic", false);
}
}
public void MagicSkills()
{
if (isPressed == false)
{
isPressed = true;
}
else
{
isPressed = false;
}
}
}
After some playing around with the code I managed to find a solution.Here I post the working code for me at least.Maybe it will help someone else too.For this to work properly you must remove the Event Trigger OnPointerUp from your button.Many thanks for the help Guilherme Schaidhauer Castro
using UnityEngine;
public class MagicSkill : MonoBehaviour
{
public GameObject hand; // Players right hand
public GameObject fireballPrefab; // Fireball particle
public Animator animator; // Casting spell animation
Vector3 fireballPos; // Adding the transform.position from the players hand
private bool isPressed = false; //Skill button (mobile)
void Update()
{
fireballPos = hand.transform.position; // Getting the position of the hand for Instatiating
if (isPressed == true)
{
animator.SetBool("Magic", true);
if (hand.transform.position.y >= 20) // Here I got the exact position of the hand for when to Instatiate the skill
{
Instantiate(fireballPrefab, fireballPos, Quaternion.identity);
isPressed = false;
}
}
else
{
animator.SetBool("Magic", false);
}
}
public void MagicSkills()
{
if (isPressed == false)
{
isPressed = true;
}
else
{
isPressed = false;
}
}
}
Keep a reference to the character's transform and use that transform to instantiate the fireball instead of using the rotation from the hand. The rotation of the hand is not changing in relation to the body, so that's why the ball is always going in the same direction.
First a screenshot of my Hierarchy :
Everything is inside a one scene name The :
The game start when Main Game is disabled. Inside Main Game sit all the game objects. The Main Menu and Game Manager are enabled.
When running the game first time, When the game start there is short animation of the player for 5 seconds. The player start from some rotating degrees on Z. Z = 50 when x and y both are 0. Then the player is rotating slowly over the Z to 0.
It's like the player is sleeping and awake up.
I'm using post processing stack by unity.
And here is the first problem. While the player is rotating on the Z and post processing effect is working if I press the Escape key it will bring me back to the Main Menu but then if I press the Escape key again it will start the game over again from the begin.
But if I'm waiting in my game for the player to finish rotating on the Z and the post processing effect is finished and then pressing on Escape it will bring the main menu and second time will resume the game from the same point.
I can't figure out why when the player is rotating and the post process is working the escape key make it start the game over again from the being ?
This is a screenshot of the game when start and after finish the rotating and the process stack :
Another problem I noticed now. After the game start using the post process and the player rotating finished if I press Escape it will go to main menu and escape again will be back to the game but for example in the second screenshot the conversation is not continue. It will return to the same point in the game but things not seems to continue like the conversation.
On the Back to main menu object I have a script attached to it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BackToMainMenu : MonoBehaviour
{
private bool _isInMainMenu = false;
public GameObject mainGame;
public GameObject mainMenu;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!_isInMainMenu)
{
// -- Code to freeze the game
mainGame.SetActive(false);
mainMenu.SetActive(true);
}
else
{
// -- Code to unfreeze the game
mainGame.SetActive(true);
mainMenu.SetActive(false);
}
_isInMainMenu = !_isInMainMenu;
}
}
}
On the Main Menu object under Main Menu I have attached this script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public GameObject mainGame;
public GameObject mainMenu;
public void PlayGame()
{
mainGame.SetActive(true);
mainMenu.SetActive(false);
}
public void QuitGame()
{
Application.Quit();
}
}
On the PLAY button I'm using this script method PlayGame from the MainMenu script.
In the Main Game object on the Player object attached to thew Player I have some scripts the controller :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
// Update is called once per frame
void Update()
{
float translatioin = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translatioin *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translatioin);
}
}
Player Lock Manager :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLockManager : MonoBehaviour
{
public PlayerCameraMouseLook playerCameraMouseLook;
public PlayerController playerController;
// Start is called before the first frame update
public void PlayerLockState(bool LockPlayer, bool LockPlayerCamera)
{
if (LockPlayer == true)
{
playerController.enabled = false;
}
else
{
playerController.enabled = true;
}
if (LockPlayerCamera == true)
{
playerCameraMouseLook.enabled = false;
}
else
{
playerCameraMouseLook.enabled = true;
}
}
}
And Mouse Lock State :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLockState : MonoBehaviour
{
public bool lockState = true;
private void Start()
{
LockState(lockState);
}
private void Update()
{
}
public void LockState(bool lockState)
{
if (lockState == false)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
else
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
}
}
Under the Player as child I have the Player Camera object and attached to the Player Camera also some scripts :
Player Camera Mouse Look :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraMouseLook : MonoBehaviour
{
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
private GameObject player;
private Vector2 mouseLook;
private Vector2 smoothV;
// Use this for initialization
void Start()
{
player = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up);
}
}
And Depth Of Field script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
public class DepthOfField : MonoBehaviour
{
public GameObject player;
public PostProcessingProfile postProcessingProfile;
public bool dephOfFieldFinished = false;
public PlayerLockManager playerLockManager;
private Animator playerAnimator;
private float clipLength;
// Start is called before the first frame update
void Start()
{
playerAnimator = player.GetComponent<Animator>();
AnimationClip[] clips = playerAnimator.runtimeAnimatorController.animationClips;
foreach (AnimationClip clip in clips)
{
clipLength = clip.length;
}
var depthOfField = postProcessingProfile.depthOfField.settings;
depthOfField.focalLength = 300;
StartCoroutine(changeValueOverTime(depthOfField.focalLength, 1, clipLength));
postProcessingProfile.depthOfField.settings = depthOfField;
}
IEnumerator changeValueOverTime(float fromVal, float toVal, float duration)
{
playerLockManager.PlayerLockState(true, true);
float counter = 0f;
while (counter < duration)
{
var dof = postProcessingProfile.depthOfField.settings;
if (Time.timeScale == 0)
counter += Time.unscaledDeltaTime;
else
counter += Time.deltaTime;
float val = Mathf.Lerp(fromVal, toVal, counter / duration);
dof.focalLength = val;
postProcessingProfile.depthOfField.settings = dof;
yield return null;
}
playerAnimator.enabled = false;
dephOfFieldFinished = true;
}
}
I have under Main Game also a object name Openning Scene and attached to it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpeningCutscene : MonoBehaviour
{
public NaviConversations naviConversation;
public DepthOfField dephOfField;
// Update is called once per frame
void Update()
{
if (dephOfField.dephOfFieldFinished == true)
{
naviConversation.PlayNaviConversation(0);
dephOfField.dephOfFieldFinished = false;
}
}
}
It's a bit long but everything is connected.
The game start with the main menu.
When clicking the PLAY button a new game start.
When a new game start the script Depth Of Field is in action using the post processing. Also I'm locking the player so the player will not be able to move either the mouse or the player it self.
When the depth of field script finished his work a conversation between Navi and the Player start. When a conversation is in action the player can move the mouse 360 degrees but cant move the player to any direction.
When the conversation ended the player can also move to any direction.
Problems :
When start a new game while the depth of field script is in work pressing escape will bring the main menu but pressing escape again will not resume the depth of field script but will start it all over again.
When waiting the conversation to end if not pressing escape until the conversation ended then when moving around and pressing escape it will bring the main menu and again will resume the game from the same point.
The problems is when the game is doing something like the depth of field it will start the game over again instead resuming or when in conversation in the middle the conversation will never continue.
The ides with the escape key is once to get to main menu and second to resume the game.
The PLAY button is what should only start a new game and not the escape key.
It's a bit long but everything is connected.
I can't see which objects you hooked up to mainGame and mainMenu in BackToMainMenu from your hierarchy, so I have some conjecture in here.
Sounds like you want Escape key to pause, and unpause, as well as bring up a menu, you want Play button in the menu to restart your game.
However, in both MainMenu and BackToMainMenu you use the same code:
mainGame.SetActive(true);
mainMenu.SetActive(false);
This just turns the objects on and off in the hierarchy. This means the first time you do one of these things, all objects turned on under the gameobject referenced by mainGame will run their Awake and Start methods, but not the second time. Also depending which objects are active (like I said I can't fully see what component is on what object and which objects are referenced by which serialized field) you might be able to introduce a state error on BackToMainMenu._isInMainMenu because that field isn't changed when you hit play. Here is a fantastic image showing execution timeline in Unity:
Monobehavior Timeline
In summary:
The second time you run the game DepthOfField won't call Start a second time. Instead try OnEnable.
The conversation and game logic is not shown in your post, but likely it also is initializing in Start and won't run a second time.
A bit of trickiness around Coroutines is likely to blame here. When DepthOfField starts it also starts a coroutine. That coroutine keeps going even if you set the gameobject inactive. So you run the game once, that coroutine starts, you turn the object off when you hit escape, the coroutine finishes, you hit play again, but DepthOfField.dephOfFieldFinished == true and your playerAnimator is disabled and won't enable again.
Also 5. This kind of behavior can be tricky and is usually dependent on what else you have going on in your scene. Since you have one scene with everything at once, you need to watch out for putting stuff in Awake and Start since this will only run once. Instead you can try a number of things, my favorite is usually to set up a singleton or static class that works as a state machine for the whole scene. It will call custom Initialize functions in your behaviors instead of using Awake and Start.
Keep references to your coroutines by doing things like:
private Coroutine depthOfFieldRoutineRef;
private OnEnable()
{
if (depthOfFieldRoutineRef != null)
{
StopCoroutine(depthOfFieldRoutineRef);
}
depthOfFieldRoutineRef =
StartCoroutine(changeValueOverTime
(depthOfField.focalLength, 1, clipLength));
// Don't forget to set depthOfFieldRoutineRef to null again at the end of routine!
}
For simple pausing behavior you can also try setting Time.timescale = 0; and back to 1 when you want play to resume.
Less a question than a set of problems, hopefully this set of solutions helps!
Tutorial on Awake and Start
Docs on Coroutines
Docs on Time.timescale
I want to start an animation when my player enters a specific distance to an object, but my animation isn't starting. Any suggestion as to why it isn't starting?
Here is the code that I have so far:
using UnityEngine;
using System.Collections;
public class MoveTo : MonoBehaviour {
public Transform Player;
public Transform goal;
public Animator ani;
public Animator ani2;
// Use this for initialization
void Start ()
{
ani.Stop (); // this stop function is working accurate
ani2.Stop ();
}
void Update()
{
float dist = Vector3.Distance (Player.position, transform.position);
if (dist < 5)
{
ani.Play("Horse_Walk");// this is not working (Horse_Walk) is a state name
ani2.Play("Horse_Run");
pstart();
}
}
void pstart(){
NavMeshAgent agent=GetComponent<NavMeshAgent>();
agent.destination = goal.position;
}
}
There can be several reason including (1) wrong string (2) string name not matched and finally which seems more appropriate reason is that you playing animation again. you should need be call it with bool
bool isPlayAnim =true;
void Update()
{
float dist = Vector3.Distance (Player.position, transform.position);
if (dist < 5 && isPlayAnim)
{
isPlayAnim = false;//again true it on you specfic event
ani.Play("Horse_Walk");// this is not working (Horse_Walk) is a state name
ani2.Play("Horse_Run");
pstart();
}
}
isPlayAnim bool you can make and use to play animation only one time and again true it in specific event.
Or
Alternatively you need to use collider and its event to run this update code. I guess it is best way to do this job .
I have been following the Learn To Code By Making Games (Block breaker game section) course from Udemy and I'm on about lecture 80 and I just realised that I can click my mouse more than once, and it will start my ball moving again, while in game and while the ball is already moving. I hope you know what I'm trying to say. So basically I want the ball to fire when the mouse is clicked, and then not have any effect after that. But also keep into consideration, the game has lives, so once one life is lost, so the game should basically reset so the player can continue with the game (like atari breakout does). Anyway, any ideas on how I could make it only? (Below is my code for the ball)
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddle;
private Vector3 paddleToBallVector;
private bool hasStarted = false;
private int speed;
//private Vector3 changeSpeed;
private Vector3 resume;
public Collider2D pauseCollider;
void Start () {
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = this.transform.position - paddle.transform.position;
}
void Update () {
if(!hasStarted) {
this.transform.position = paddle.transform.position + paddleToBallVector;
}
}
public void startGame () {
//print ("Mouse Has Been Clicked\n Launching Ball");
hasStarted = true;
this.rigidbody2D.velocity = new Vector2 (0f, 200f);
}
}