AI wandering state doesn't seem to work right - c#

I've been trying to make an AI system that would follow specific waypoints and once the player character walks into the trigger zone, the AI would start chasing the character and vice versa.
It seems that this does kind of work, but the AI only moves to one waypoint and then stops until the player walks into the trigger zone; after which if the player walks out, it again only goes to one waypoint then stops again.
I have multiple waypoints and I just want it to keep cycling through them, but it just goes to one of them and stops.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngineInternal;
public class AILocomotion : MonoBehaviour
{
public Transform playerTransform;
NavMeshAgent agent;
Animator animator;
public float maxTime = 1.0f;
public float maxDistance = 1.0f;
float timer = 0.0f;
bool moveTowards = false;
bool followWaypoint = false;
float deaccel= 0.5f;
float calVelocity = 0.0f;
public Transform[] points;
private int destPoint = 0;
public string animState;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
moveTowards = false;
followWaypoint = true;
}
// Update is called once per frame
void Update()
{
if (moveTowards == true)
{
timer -= Time.deltaTime;
if (timer < 0.0f)
{
float sqDistance = (playerTransform.position - agent.destination).sqrMagnitude;
if (sqDistance > maxDistance * maxDistance)
{
agent.destination = playerTransform.position;
}
}
animator.SetFloat("Speed", agent.velocity.magnitude);
}
else if (!moveTowards && agent.velocity.magnitude>0.0f)
{
calVelocity = agent.velocity.magnitude;
calVelocity -= Time.deltaTime * deaccel;
animator.SetFloat("Speed", calVelocity);
}
//CHECKS IF BOTH CONDITIONS HAVE MET AND RUNS THE WAYPOINT FOLLOWING CODE
if (followWaypoint == true && (!agent.pathPending && agent.remainingDistance < 1.0f))
{
GotoNextPoint();
}
Debug.LogError("Bool" + followWaypoint);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
moveTowards = true;
//DISABLES THE WAYPOINT FOLLOWING CODE TO RUN THE CHASE CODE INSTEAD
followWaypoint = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
moveTowards = false;
//RE-ENABLES THE WAYPOINT FOLLOWING CODE ONCE THE PLAYER LEAVES THE TRIGGER AREA
followWaypoint = true;
}
}
//THIS IS THE WAYPOINT FOLLOWING CODE
void GotoNextPoint()
{
animator.SetFloat("Speed", agent.velocity.magnitude);
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
Debug.LogError("DestPoint = " + destPoint);
// Choose the next point in the array as the destination.
// cycling to the start if necessary.
destPoint = Random.Range(0, points.Length);
}
}

Use OnTriggerStay instead of OntriggerEnter and Try

Related

Stopping player's movement upon clicking dialogue - Unity

I have a problem where when I click the dialogue while passing through an NPC or while moving, the character will continue moving in the direction of the joystick before it is set active to false.
I have tried setting the horizontal and vertical input to zero and even the movement direction which is the calculated horizontal and vertical input, but the character still moves in the last direction it was moving. I'm not sure if the if statement that I created is correct but that was the idea that came to mind.
This is the script for the movement of the player:
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private GameObject interactButton;
public float speed, rotationSpeed, ySpeed, originalStepOffset;
public Joystick joystick;
private Animator animator;
private CharacterController characterController;
void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
originalStepOffset = characterController.stepOffset;
}
// Update is called once per frame
void FixedUpdate()
{
float horizontalInput = joystick.Horizontal;
float verticalInput = joystick.Vertical;
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
movementDirection = Quaternion.Euler(0, 45f, 0) * movementDirection;
float magnitude = Mathf.Clamp01(movementDirection.magnitude) * speed;
movementDirection.Normalize();
ySpeed += Physics.gravity.y * Time.deltaTime;
if(characterController.isGrounded)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
}
else
{
characterController.stepOffset = 0;
}
Vector3 velocity = movementDirection * magnitude;
velocity = AdjustVelocityToSlope(velocity);
velocity.y += ySpeed;
//transform.Translate(movementDirection * speed * Time.deltaTime, Space.World);
characterController.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{
if(EventSystem.current.currentSelectedGameObject != null)
{
if (EventSystem.current.currentSelectedGameObject.name == "InteractButton")
{
horizontalInput = 0f;
verticalInput = 0f;
movementDirection = Vector3.zero;
animator.SetBool("IsMoving", false);
}
else
{
//transform.forward = movementDirection;
animator.SetBool("IsMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
}else
{
animator.SetBool("IsMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
} else
{
animator.SetBool("IsMoving", false);
}
}
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
{
var ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray, out RaycastHit hitInfo, 0.2f))
{
var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var adjustedVelocity = slopeRotation * velocity;
if(adjustedVelocity.y < 0)
{
return adjustedVelocity;
}
}
return velocity;
}
}
The function that will make the button to set active to true and add its onclick listeners is a trigger enter function and a trigger exit for setting it active to false and removing the onclick listeners. It is attached to multiple NPC whenever I go near them, the button will appear. In case someone needs to see the script for the button here it is, but I'm gonna paste only until the related part:
using System;
using System.Collections;
using System.Drawing;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[System.Serializable]
public class DialogueManager : MonoBehaviour
{
/*
// NPC INTERACTION
private static DialogueManager buttonOwner;
private Camera mainCamera;
*/
// NPC DATA
private Character characterJson;
// FOR DIALOG FLOW
private int multiDialogCycle;
public static int dialogId;
// GAME OBJECTS REFERENCE
[SerializeField] private GameObject dialogBox, addPanel, m1, id, darkPanel;
[SerializeField] private Button nameBtn, choiceOneBtn, choiceTwoBtn;
[SerializeField] private TextMeshProUGUI npcName, choiceOne, choiceTwo, dialogName, dialogMessage, addedText;
[SerializeField] private TextAsset characterData;
// FOR MULTIPLE DIALOGUES WITHOUT CHOICES
private bool clickEnable;
private string[] multiDialog;
// CHOICES STORAGE
private static Choice[] choices = new Choice[2];
//private TryInstantiate tryInstantiate = new TryInstantiate();
private GameData gameData;
private void Start()
{
// LOAD NPC DATA
characterJson = JsonUtility.FromJson<Character>(characterData.text);
// SET DEFAULT VALUE
//dialogId = 0;
multiDialogCycle = 0;
clickEnable = false;
// SET SCRIPTABLE OBJECT FOR DATA STORAGE
gameData = Resources.Load<GameData>("GameData/GameData");
}
private void Update()
{
if(nameBtn.gameObject.activeInHierarchy)
{
if(Input.GetKey(KeyCode.Space))
{
EventSystem.current.SetSelectedGameObject(nameBtn.gameObject);
}
}
// FOR ENDING DIALOGUES WITHOUT CHOICES
if (Input.GetMouseButtonDown(0) && clickEnable == true)
{
if (multiDialog.Length > 1 )
{
if(getDialog(dialogId).choices.Length == 0 && multiDialogCycle == multiDialog.Length - 1)
{
addArticles();
addObjectives();
addClues();
closeDialog();
Debug.Log(getDialog(dialogId).minigame);
}
else
{
if(getDialog(dialogId).minigame != "" && multiDialogCycle < multiDialog.Length - 1)
{
if(!gameData.idShown)
{
darkPanel.SetActive(true);
id.SetActive(true);
}
}else
{
multiDialogCycle++;
loadCharacterData();
}
}
}
else
{
if (getDialog(dialogId).minigame != "")
{
if(getDialog(dialogId).minigame == "spot_object")
{
m1.SetActive(true);
}
dialogBox.SetActive(false);
gameData.dialogActive = false;
}
else
{
addArticles();
addObjectives();
addClues();
closeDialog();
}
}
}
if(gameData.idShown)
{
multiDialogCycle++;
loadCharacterData();
gameData.idShown = false;
}
if (gameData.dialogActive && dialogId != 0 && getDialog(dialogId).minigame != "" && !gameData.loadedData)
{
updateID();
loadCharacterData();
}
// FOR NPC NAMES
/*
if (buttonOwner != this) return;
if (!mainCamera) mainCamera = Camera.main;
var position = mainCamera.WorldToScreenPoint(head.position + offset);
//uiUse.transform.position = position;
nameBtn.transform.position = position;
*/
}
private void OnTriggerEnter(Collider collisionInfo)
{
if (collisionInfo.CompareTag("Player"))
{
nameBtn.gameObject.SetActive(true);
nameBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("InteractionAsset/DIALOGUE");
nameBtn.transform.GetChild(0).GetComponent<TextMeshProUGUI>().color = new Color32(75,75,75,255);
//buttonOwner = this;
nameBtn.onClick.RemoveListener(onNameClick);
nameBtn.onClick.AddListener(onNameClick);
choiceOneBtn.onClick.RemoveListener(onChoiceClick);
choiceOneBtn.onClick.AddListener(onChoiceClick);
choiceTwoBtn.onClick.RemoveListener(onChoiceClick);
choiceTwoBtn.onClick.AddListener(onChoiceClick);
npcName.text = characterJson.name;
}
}
private void OnTriggerExit(Collider collisionInfo)
{
if (collisionInfo.CompareTag("Player"))
{
nameBtn.onClick.RemoveListener(onNameClick);
choiceOneBtn.onClick.RemoveListener(onChoiceClick);
choiceTwoBtn.onClick.RemoveListener(onChoiceClick);
nameBtn.gameObject.SetActive(false);
//buttonOwner = null;
}
}
// DIALOGUE SYSTEM
public void onNameClick()
{
nameBtn.gameObject.SetActive(false);
dialogBox.SetActive(true);
gameData.dialogActive = true;
FindObjectOfType<AudioManager>().Play("ButtonSound");
if (dialogBox.activeInHierarchy)
{
if (dialogMessage != null && dialogName != null)
{
loadCharacterData();
interactedNPC();
}
else
{
// Debug.Log("null dialog message");
}
}
}
public void updateID()
{
if (gameData.win && !gameData.loadedData)
{
dialogId = gameData.targetId_1;
gameData.loadedData = true;
}
else if (!gameData.win && !gameData.loadedData)
{
dialogId = gameData.targetId_2;
gameData.loadedData = true;
}
}
How can I atleast reset the Joystick's position. I am currently using the Joystick Pack from Unity Asset Store.
I for one would start the Conversation flow from the dialogue step. Once the dialog starts, you can either set a bool or any other type of property (even a referenced object such as the dialogue itself or the NPC that it's chatting to, but a bool is simpler) that would be a marker. Simply put: public bool IsTalking;. This could be in PlayerMovement or the main Player-like component (or SO) which you can access from PlayerMovement via GameObject or a different public variable.
Next, once the dialogue mechanism starts, you can set the marker (such as the bool or NPC ref) in the Player-like component or PlayerMovement to true, false when the chatting stops.
Then in PlayerMovement.FixedUpdate() you can just stop code execution:
// Update is called once per frame
void FixedUpdate()
{
// or if (Player.IsTalking == true) if you want the marker there,
// but you can have it inside PlayerMovement if you wish as long as
// you can reference it without overhead - meaning don't do crazy
// stuff like searching all game objects/components for PlayerMovement)
if (IsTalking == true)
{
if (characterController != null)
characterController.Move(Vector3.zero);
if (animator != null)
animator.SetBool("IsMoving", false);
return;
}
// everything starts executing once the player stops talking to NPCs
float horizontalInput = joystick.Horizontal;
float verticalInput = joystick.Vertical;
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
movementDirection = Quaternion.Euler(0, 45f, 0) * movementDirection;
float magnitude = Mathf.Clamp01(movementDirection.
As I understood you dialogue window is modal. Depending on your needs you can stop the internal increment of time progression with
Time.timeScale = 0f;
But this will stop e.g. (UI) animations and the proper function of the event system, so some people use
Time.timeScale = 0.0001f;
This has the drawback that the gameplay continues slowly and our player hero could be hit by a very slow rocket while the user was resting in a pause screen.

MissingReferenceException Error Occurs with Unity's New Input System when using "buttonEast / rightButton"

I'm getting this error in Unity after pressing the relative "eastButton":
Error: MissingReferenceException while executing 'canceled' callbacks of 'Gameplay/Block[/DualShock4GamepadHID/buttonEast,/Mouse/rightButton]'
UnityEngine.InputSystem.InputActionAsset:OnDestroy () (at Library/PackageCache/com.unity.inputsystem#1.0.2/InputSystem/Actions/InputActionAsset.cs:794)
I'm attempting to set up 'block' functionality within my game which simply locks the player's movement then disables my "canBeHit" bool which will then put the player in a state where they cannot be hit, however upon interacting with the eastButton all player movement is completely locked preventing you from moving, however rotation is still possible. Another part I'd like to note is that I'm working with the Mirror package so I can network my game, unsure if this could also be causing problems. Would really appreciate if someone could point out where I've gone wrong, I'd be very grateful.
PlayerMovementTest.cs
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Mirror;
public class PlayerMovementTest : NetworkBehaviour
{
[Header ("Default")]
Rigidbody rb;
PlayerInput playerInput;
PlayerControls playerInputActions;
public bool isStunned;
public float stunLockTimer;
public float stunLockTimerEnd;
public Animator animations;
public ParticleSystem stunParticle;
[Header("Movement")]
public float speed;
public float baseSpeed;
[Header ("Dodge")]
public float dodgeMultiplier;
public float dodgeTimer;
public float dodgeTimerMax;
public int dodgeCheck;
public float howLongDodgeLast;
public bool isDodging;
public GameObject enemyPlayer;
public GameObject dodgeParticle;
[Header("Attack")]
public int attackReset;
public float attackTimer;
public float attackTimerResetMax;
public float attackPlayingTimerEnd;
public float lungeAttackLungeMultiplier;
public bool isAttacking;
public GameObject attackParticle;
public GameObject attackHitboxObject;
public GameObject lungeAttackHitboxObject;
[Header("Block")]
public bool canBeHit;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
rb = GetComponent<Rigidbody>();
playerInputActions = new PlayerControls();
// freezes rotation
rb.freezeRotation = true;
// enables the input controller
playerInputActions.Gameplay.Enable();
playerInputActions.Gameplay.Attack.performed += Attack;
playerInputActions.Gameplay.Attack.canceled += Attack;
playerInputActions.Gameplay.Dodge.performed += Dodge;
playerInputActions.Gameplay.Block.canceled += Block;
playerInputActions.Gameplay.Block.performed += Block;
isStunned = false;
}
public void Update()
{
if (!isLocalPlayer)
return;
// dodge timers and reset
if (dodgeCheck != 1)
dodgeTimer += Time.deltaTime;
if (dodgeTimer > dodgeTimerMax)
{
dodgeCheck += 1;
dodgeTimer = 0;
}
// turns on the slide particle when sliding
if (isDodging == true)
dodgeParticle.SetActive(enabled);
else
dodgeParticle.SetActive(false);
// sets how long the EFFECT of the dash lasts for
// not how long the player will move but how long
// they will be able to collide with another player
// and stun them
if (dodgeTimer > howLongDodgeLast)
isDodging = false;
// attack timers and reset
if (attackReset != 1)
{
attackTimer += Time.deltaTime;
}
// after a certian amount of time the player attack goes away
if (attackTimer > attackPlayingTimerEnd)
{
//attackParticle.SetActive(false);
attackHitboxObject.SetActive(false);
lungeAttackHitboxObject.SetActive(false);
}
if (attackTimer > attackTimerResetMax)
{
attackReset += 1;
attackTimer = 0;
}
// makes it so attacks dont go into the minuses
if (attackReset < 0)
attackReset = 0;
// when player gets stunned it starts a timer where they cannot do any moves
if (isStunned == true)
{
stunLockTimer += Time.deltaTime;
if (stunLockTimer > stunLockTimerEnd)
{
isStunned = false;
stunLockTimer = 0f;
speed = baseSpeed;
}
}
// chanegs between animations depending on how fast the player is moving
if (rb.velocity.magnitude > 4f)
animations.SetFloat("Walk", rb.velocity.magnitude);
if (rb.velocity.magnitude < 4f)
animations.SetFloat("Idle", rb.velocity.magnitude);
// if player is stunned then play stun particle
if (isStunned == true)
stunParticle.Play();
else
stunParticle.Stop();
//edited out the print as it got distracting
// print(rb.velocity.magnitude);
}
// for dodge stun
public void OnCollisionEnter(Collision col)
{
// if you hit a player
if( col.gameObject.tag == "Player")
{
// and in dodge, number picked just as an idea of how long the dash could last
if (dodgeTimer > .1f && dodgeTimer < 1f)
{
// sets the player hit as a gameobject that cabn be affected in script
enemyPlayer = col.gameObject;
// decreases speed (or in this case increases as i cannot test 2 players
enemyPlayer.GetComponent<PlayerMovementTest>().speed = 0;
enemyPlayer.GetComponent<PlayerMovementTest>().isStunned = true;
}
}
}
public void FixedUpdate()
{
// calls movement function
Movement();
}
public void Attack(InputAction.CallbackContext context)
{
if (attackReset == 1 && isStunned == false)
{
isAttacking = true;
attackHitboxObject.SetActive(enabled);
attackReset -= 1;
animations.SetTrigger("Attack");
attackParticle.SetActive(enabled);
}
//if (attackReset == 1 && isStunned == false)
/*
if (context.performed)
{
print("attack start");
}
if (context.canceled)
{
// for luneg attack
rb.AddForce(transform.forward * lungeAttackLungeMultiplier, ForceMode.Impulse);
isAttacking = true;
lungeAttackHitboxObject.SetActive(enabled);
attackReset -= 1;
// animations.SetTrigger("Attack");
// attackParticle.SetActive(enabled);
print("attack end");
}*/
}
public void Dodge(InputAction.CallbackContext context)
{
// checks if the player has a dodge
if (dodgeCheck == 1 && isStunned == false)
{
// used for lockign slide in place and particle effect
isDodging = true;
// adds a force to the player, spped can be adjusted with dodgeMultiplier
rb.AddForce(transform.forward * dodgeMultiplier, ForceMode.Impulse);
// removes dodge
dodgeCheck -= 1;
animations.SetTrigger("Slide");
}
}
public void Block(InputAction.CallbackContext context)
{
if (isStunned == false)
{ // when you hold the button, you lose your speed
if (context.performed)
{
canBeHit = false;
speed = 3;
animations.SetBool("Block", true);
}
// when you release it your speed goes back to normal
if (context.canceled)
{
canBeHit = true;
speed = 15;
animations.SetBool("Block", false);
}
}
}
public void Movement()
{
// used for locking slide in place, no rotation
if (isDodging == false)
{
//player movement, can only use vector2 for controller so we use a vector3
// but store the x and z in a vector 2
Vector2 inputVector = playerInputActions.Gameplay.Walk.ReadValue<Vector2>();
Vector3 tempVec = new Vector3(inputVector.x, 0, inputVector.y);
// adds force to the vector, do this seperately so we can use
//the variable for the player rotation
rb.AddForce(tempVec * speed, ForceMode.Force);
if (tempVec != Vector3.zero)
{
// finds the direction the player is moving
Quaternion targetRotation = Quaternion.LookRotation(tempVec);
// rotates players towards the way they are facing
targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 360 * Time.fixedDeltaTime);
rb.MoveRotation(targetRotation);
}
}
}
}
If any additional information is needed to solve my problem I'm more than happy to submit any necessary info.

2d sprite goes too fast with transform.translate in C# script (unity)

Sup. My sprite goes too fast when I press the C button. It's a wolf that will lunge as an attack. But it just goes from one spot to the next, and I got the idea that I'm simply using the wrong kind of code entirely. I'm guessing it has to do more with Rigidbody2D = new Vector2.... but I don't know where to go from there. Here's what I'm working with currently.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class tulMoveMount : MonoBehaviour {
private Animator anim;
private Rigidbody2D rb;
private bool goRight = true;
private bool jump = false;
private bool idle = true;
public float lungeDistance;
public float lungeSpeed;
public float lungeHeight;
void Start ()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void Update ()
{
HandleCommands ();
}
void HandleCommands()
{
if (!jump && goRight && Input.GetKeyDown (KeyCode.C)) {
idle = false;
jump = true;
anim.SetTrigger ("jump");
rb = transform.Translate (lungeSpeed + lungeDistance, lungeHeight, 0); // HERE
idle = true;
jump = false;
anim.SetTrigger ("idle");
}
if (!jump && !goRight && Input.GetKeyDown (KeyCode.C)) {
idle = false;
jump = true;
anim.SetTrigger ("jump");
rb = transform.Translate (lungeSpeed + -lungeDistance, lungeHeight, 0); // HERE
idle = true;
jump = false;
anim.SetTrigger ("idle");
}
}
}
Multiplying the translation by Time.DeltaTime will smooth the movement over many frames, and you will then need to tweak only the lungeSpeed to get the speed you want.
rb = transform.Translate ((lungeSpeed + lungeDistance, lungeHeight, 0)*Time.deltaTime);
To get a smooth translation from one point to another you can use Lerp inside a Corouting.
In Lerp the first parameter is the start position and the second the targe. The third parameter is a float between 0 and 1. If it's 0, you get the first parameter in Lerp. If it's 1, you get the second. If 0.5 a middle point between both and so on...
So what you need to do is to start a courutine, which will be independent of the fps rate, and will move your GameObject in a constant speed defined by the distance between start-target and the time you want it takes to move from one to the other.
public class WolfMovement : MonoBehaviour {
Vector3 start;
Vector3 target;
float lungeSpeed = .8f;
float lungeDistance = 5;
private IEnumerator coroutine;
void Update () {
if(Input.GetKeyDown(KeyCode.M) )
{
start = transform.position;
target = new Vector3(transform.position.x + lungeDistance,transform.position.y , transform.position.z);
coroutine = MoveObject(start,target,lungeSpeed);
StartCoroutine(coroutine);
}
}
IEnumerator MoveObject (Vector3 start, Vector3 target, float speed){
float i = 0.0f;
float rate = 1.0f/speed;
while (i < 1.0) {
i += Time.deltaTime * rate;
transform.position = Vector3.Lerp(start, target, i);
yield return null;
}
}
}

Unity 5.3.5f Not Registering Space Bar Being Pressed

So I have a player movement script that is supposed to allow the player to double jump by counting the number of times the space bar is pressed. It was working a couple versions early, but now has just randomly stopped working. No idea why it randomly stopped working.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float jumpPower = 2f;
[SerializeField]
public bool isGrounded = true;
[SerializeField]
private LayerMask groundLayer, waterLayer;
private int numSpacePress = 0;
private Rigidbody2D rb;
// Ground Checker Stuff
public Transform groundChecker;
public float groundCheckRadius = 0.1f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && numSpacePress < 2)
{
numSpacePress++;
Debug.Log("Num spaces pressed = " + numSpacePress);
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
if (numSpacePress >= 2)
{
isGrounded = false;
}
}
Collider2D[] hits = Physics2D.OverlapCircleAll(groundChecker.position, groundCheckRadius);
foreach (Collider2D hit in hits)
{
if (hit.gameObject != gameObject)
{
Debug.Log("Hit = " + hit.gameObject.name);
if (((1 << hit.GetComponent<Collider2D>().gameObject.layer) & groundLayer) != 0)
{
isGrounded = true;
numSpacePress = 0;
}
}
}
}
}
What's Happening Gif, clearly in the gif I am pressing space twice, but it is only printing the number of spaces pressed by one. If the player is on the ground the number of spaces pressed is reset, but the gif also shows that the player is not touching the ground. Why is the space bar not being registered when pressed?
What was happening was a hit was occurring just after the space bar was pressed, which reset the variable counting the number of spaces pressed.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float jumpPower = 2f;
[SerializeField]
public bool isGrounded = true;
[SerializeField]
private LayerMask groundLayer, waterLayer;
private int numSpacePress = 0;
private Rigidbody2D rb;
// Ground Checker Stuff
public Transform groundChecker;
public float groundCheckRadius = 0.1f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
Collider2D[] hits = Physics2D.OverlapCircleAll(groundChecker.position, groundCheckRadius);
foreach (Collider2D hit in hits)
{
if (hit.gameObject != gameObject)
{
Debug.Log("Hit = " + hit.gameObject.name);
if (((1 << hit.GetComponent<Collider2D>().gameObject.layer) & groundLayer) != 0)
{
isGrounded = true;
numSpacePress = 0;
}
}
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && numSpacePress < 2)
{
numSpacePress++;
Debug.Log("Num spaces pressed = " + numSpacePress);
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
if (numSpacePress >= 2)
{
isGrounded = false;
}
}
}
}

Detect if enemy is facing Player

I am making a game in which if the distance is less than 2 and the enemy is facing the player, text comes up with a restart option. In the update there is an if and else statement which should detect if the enemy is behind or in front of the player. However, the in front option seems to be called once the distance is less than 2, regardless of if the player is facing the npc.
This script is attached to the Enemy:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EnemyFollow : MonoBehaviour {
Transform player;
Transform enemy;
public GameObject EnemyCaughtCam;
public Text SheCaughtYou;
public GameObject Restart;
public float Speed = 3f;
public float rotS = 3f;
public float sT = 3f;
NavMeshAgent nav;
float timeTillOptionsMenu = 3.0f;
// Use this for initialization
void Awake() {
player = GameObject.FindGameObjectWithTag ("MainCamera").transform;
//enemy = GameObject.FindGameObjectWithTag ("Enemy").transform;
nav = GetComponent<NavMeshAgent> ();
EnemyCaughtCam.SetActive(false);
SheCaughtYou.text = "";
}
// Update is called once per frame
void Update () {
nav.SetDestination (player.position);
DistanceDeath();
if (npcIsFacingPlayer (player)&& !playerIsFacingNpc(player))
print ("Behind");
else if (npcIsFacingPlayer (player)&& playerIsFacingNpc(player))
print ("In Front");
DistanceDeath ();
}
public void DistanceDeath(){
float distance = Vector3.Distance(player.transform.position,
transform.position);
if (distance < 2 ){
EnemyCaughtCam.SetActive(true);
SheCaughtYou.text = "SHE CAUGHT YOU!";
timeTillOptionsMenu -= Time.deltaTime;
if(timeTillOptionsMenu < 0)
{
Restart.SetActive(true);
}
}
}
public bool npcIsFacingPlayer(Transform other)
{
Vector3 toOther =
other.position - transform.position;
return (Vector3.Dot(toOther, transform.forward) > 0);
}
public bool playerIsFacingNpc(Transform other)
{
Vector3 toOther =
transform.position - other.position;
return (Vector3.Dot(toOther, other.forward) > 0);
}
}
First, you are missing some brackets, second there is an stra DistanceDeathcall, here is how your function Update is read:
// Update is called once per frame
void Update () {
nav.SetDestination (player.position);
/** what does the call do here? */
DistanceDeath();
if (npcIsFacingPlayer (player)&& !playerIsFacingNpc(player))
print ("Behind");
else if (npcIsFacingPlayer (player)&& playerIsFacingNpc(player))
print ("In Front");
/** are you missing brackets here? Distance Death is always called */
DistanceDeath ();
}

Categories