Need help in fixing a glitch with my Unity hiding script - c#

I'm making a prototype for a horror game in Unity, and I'm trying to create a script that makes it so you can hide from the enemy in certain objects.
The intention is to make it that the camera lerps to the hiding spot in a smooth animation, and then will lerp the camera back to the player's position on exit.
I have it working so the player can enter and exit with a lerp, but the issue is that after exiting the hiding spot, interactions with any object cause the camera to sporadically rotate, rather than doing the intended interaction.
Here is my hide script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hidey : MonoBehaviour, IInteractable
{
[SerializeField] private Camera mainCamera;
[SerializeField] private Camera hideCamera;
[SerializeField] private float hideSpeed;
private float timer;
private Vector3 endPosition;
private Quaternion endRotation;
private Vector3 endPosition2;
private Quaternion endRotation2;
private bool canToggle;
private bool togglingLerp;
private bool hidden;
private void Awake()
{
hideCamera.enabled = false;
mainCamera.enabled = true;
canToggle = true;
togglingLerp = false;
hidden = false;
endPosition = hideCamera.transform.position;
endRotation = hideCamera.transform.rotation;
}
public void Interaction()
{
if (canToggle && !hidden)
{
hideCamera.enabled = true;
hideCamera.transform.position = mainCamera.transform.position;
hideCamera.transform.rotation = mainCamera.transform.rotation;
mainCamera.enabled = false;
endPosition2 = mainCamera.transform.position;
endRotation2 = mainCamera.transform.rotation;
togglingLerp = true;
}
else return;
}
private void Update()
{
if(togglingLerp && !hidden)
{
canToggle = false;
timer = Time.deltaTime * hideSpeed;
HideAnimation(hideCamera.transform.position, endPosition, hideCamera.transform.rotation, endRotation, timer, true, hideCamera);
}
if(hidden && Input.GetKeyDown(KeyCode.E))
{
canToggle = false;
mainCamera.enabled = true;
mainCamera.transform.position = hideCamera.transform.position;
mainCamera.transform.rotation = hideCamera.transform.rotation;
hideCamera.enabled = false;
timer = Time.deltaTime * hideSpeed;
HideAnimation(mainCamera.transform.position, endPosition2, mainCamera.transform.rotation, endRotation2, timer, false, mainCamera);
}
}
private void HideAnimation(Vector3 startPos, Vector3 endPos, Quaternion startRot, Quaternion endRot, float lerpTimer, bool isHidden, Camera currentCamera)
{
currentCamera.transform.position = Vector3.Lerp(startPos, endPos, lerpTimer);
currentCamera.transform.rotation = Quaternion.Lerp(startRot, endRot, lerpTimer);
if (Quaternion.Dot(currentCamera.transform.rotation, endRot) > .9999f && Vector3.Dot(currentCamera.transform.position, endPos) > .9999f)
{
hidden = isHidden;
togglingLerp = false;
canToggle = true;
}
}
}
I have no idea what is causing this issue at the moment.

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.

Player Jumping Twice On Lower Framerate When Mashing Space

First of all I want to apologise for my English cause I come from Poland and I'm still learning. I'm a beginner C#/Unity programmer and I have a pretty stupid/noob question/issue where player jumps twice when mashing the space. When the framerate is low for ex. 30, the problem occurs almost everytime and when framerate is for ex. 144 - hardly ever. I did some research and tried different methods. Firstly I checked whether I have all my inputs in Update and not FixedUpdate. That wasn't the problem at all. Then I tried replacing Input.GetKey to Input.GetKeyDown and GetKeyUp in Update as to ensure I have my boolean _spacePressed checked or not. That wasn't the solution too. The third thing I tried was to use Raycasting to check whether the player is grounded. With that I also checked whether when I jump, ray doesn't get checked twice. To make things clear I'm currently trying to make a 2.5D platformer. So to sum up I'm asking what could be the main issue (with input probably) where player jumps twice or even three times in a single frame when mashing space. Here's also my prototype code. I used enums to make a simple state "machine". If you have also any advice for me and my code to make things better apart from my question I would love to hear them. Thank you in advance!
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Start Properties")]
[SerializeField] private Transform _playerTransform;
[SerializeField] private Rigidbody _playerRigidbody;
private enum MovementStates { reset, idle, walking, walkingInAir, sprinting, jumping };
private MovementStates _currentState;
[Header("Player Height Adjustables")]
[SerializeField] CapsuleCollider _playerCollider;
[SerializeField] private float _reducedHeight;
private float _originalHeight;
[Header("Player Rotation Adjustables")]
[SerializeField] private float _rotationSpeed;
Quaternion _from;
Quaternion _to;
[Header("Input Properties")]
private float _xAxis;
private bool _rightLook, _leftLook;
private bool _idle, _walking, _walkingInAir, _sprinting, _reset;
private bool _leftShiftPressed;
private bool _spacePressed;
[Header("Player Movement Adjustables")]
[SerializeField] private float _walkingSpeedMultiplier;
[SerializeField] private float _maximumWalkingSpeed;
[SerializeField] private float _walkingInAirSpeedMultiplier;
[SerializeField] private float _maximumWalkingInAirSpeed;
[SerializeField] private float _sprintingSpeedMultiplier;
[SerializeField] private float _maximumSprintingSpeed;
[SerializeField] private float _deaccelerationMultiplier;
[Header("Smooth Damp Adjustables")]
[SerializeField] private float _smoothTime;
private Vector3 _currentVelocity;
private Vector3 _smoothAxis;
[Header("Player Jump Adjustables")]
[SerializeField] private float _jumpMultiplier;
private bool _jumping;
[Header("Ground Check Adjustables")]
[SerializeField] private LayerMask _groundCheck_Layer;
[SerializeField] private float _distanceGroundCheck;
private Ray _groundCheck_Ray;
private RaycastHit _groundCheck_HitInfo;
private bool _grounded;
private void Awake()
{
_originalHeight = _playerCollider.height;
}
// input and ground checks
private void Update()
{
Process_Input();
Process_Rotation();
GroundRay_Check();
}
private void Process_Input()
{
_xAxis = Input.GetAxisRaw("Horizontal");
_rightLook = (_xAxis > 0) ? _rightLook = true : _rightLook = false;
_leftLook = (_xAxis < 0) ? _leftLook = true : _leftLook = false;
if (Input.GetKeyDown(KeyCode.LeftShift)) _leftShiftPressed = true;
if (Input.GetKeyUp(KeyCode.LeftShift)) _leftShiftPressed = false;
if (Input.GetButtonDown("Jump")) _spacePressed = true;
if (Input.GetButtonUp("Jump")) _spacePressed = false;
_idle = (_xAxis == 0 && _grounded) ? _idle = true : _idle = false;
_walking = (_xAxis != 0 && !_leftShiftPressed && _grounded) ? _walking = true : _walking = false;
_walkingInAir = (_xAxis != 0 && !_grounded) ? _walkingInAir = true : _walkingInAir = false;
_sprinting = (_xAxis != 0 && _leftShiftPressed && _grounded) ? _sprinting = true : _sprinting = false;
_jumping = (_spacePressed && _grounded) ? _jumping = true : _jumping = false;
_reset = (!_idle && !_walking && !_walkingInAir && !_sprinting && !_jumping) ? _reset = true : _reset = false;
if (Input.GetKeyDown(KeyCode.Alpha1)) Application.targetFrameRate = 30;
if (Input.GetKeyDown(KeyCode.Alpha2)) Application.targetFrameRate = 60;
if (Input.GetKeyDown(KeyCode.Alpha3)) Application.targetFrameRate = 120;
if (Input.GetKeyDown(KeyCode.Alpha4)) Application.targetFrameRate = 144;
}
private void Process_Rotation()
{
if (_rightLook)
{
_from = _playerTransform.rotation;
_to = Quaternion.Euler(0f, 0f, 0f);
_playerTransform.rotation = Quaternion.Lerp(_from, _to, _rotationSpeed * Time.deltaTime);
}
else if (_leftLook)
{
_from = _playerTransform.rotation;
_to = Quaternion.Euler(0f, 180f, 0f);
_playerTransform.rotation = Quaternion.Lerp(_from, _to, _rotationSpeed * Time.deltaTime);
}
}
private void GroundRay_Check()
{
_groundCheck_Ray.origin = _playerTransform.position;
_groundCheck_Ray.direction = Vector2.down;
Debug.DrawRay(_playerTransform.position, Vector2.down * _distanceGroundCheck, Color.green);
_grounded = Physics.Raycast(_groundCheck_Ray, out _groundCheck_HitInfo, _distanceGroundCheck, _groundCheck_Layer, QueryTriggerInteraction.Ignore);
}
// movement by states
private void FixedUpdate()
{
Process_States();
}
private void Process_States()
{
if (_idle) _currentState = MovementStates.idle;
if (_walking) _currentState = MovementStates.walking;
if (_walkingInAir) _currentState = MovementStates.walkingInAir;
if (_sprinting) _currentState = MovementStates.sprinting;
if (_jumping) _currentState = MovementStates.jumping;
if (_reset) _currentState = MovementStates.reset;
switch (_currentState)
{
case MovementStates.idle:
Process_Idle();
break;
case MovementStates.walking:
Process_Walking();
break;
case MovementStates.walkingInAir:
Process_WalkingInAir();
break;
case MovementStates.sprinting:
Process_Sprinting();
break;
case MovementStates.jumping:
Process_Jumping();
break;
case MovementStates.reset:
print("resetting");
return;
}
}
private void Process_Idle()
{
print("currently idle");
_playerCollider.height = _originalHeight;
Deaccelerate(_deaccelerationMultiplier);
}
private void Process_Walking()
{
print("currently walking");
Move(_walkingSpeedMultiplier, _maximumWalkingSpeed, ForceMode.Force);
}
private void Process_WalkingInAir()
{
print("currently walking in air");
Move(_walkingInAirSpeedMultiplier, _maximumWalkingInAirSpeed, ForceMode.Force);
}
private void Process_Sprinting()
{
print("currently sprinting");
Move(_sprintingSpeedMultiplier, _maximumSprintingSpeed, ForceMode.Force);
}
private void Process_Jumping()
{
print("currently jumping");
Jump(_jumpMultiplier, ForceMode.VelocityChange);
_spacePressed = false;
}
// movement functions
private void Move(float _speedMultiplier, float _maximumSpeed, ForceMode _forceMode)
{
CapMaximumSpeed(_maximumSpeed);
Vector2 _getAxisDirection = _xAxis * Vector2.right;
Vector2 _normalizeAxis = _getAxisDirection.normalized;
Vector2 _multiplyAxis = _normalizeAxis * _speedMultiplier * Time.deltaTime;
_smoothAxis = Vector3.SmoothDamp(_multiplyAxis, _smoothAxis, ref _currentVelocity, _smoothTime);
_playerRigidbody.AddForce(_smoothAxis, _forceMode);
}
private void CapMaximumSpeed(float _maximumSpeed)
{
float _cappedXVelocity = Mathf.Min(Mathf.Abs(_playerRigidbody.velocity.x), _maximumSpeed) * Mathf.Sign(_playerRigidbody.velocity.x);
float _cappedYVelocity = _playerRigidbody.velocity.y;
_playerRigidbody.velocity = new Vector3(_cappedXVelocity, _cappedYVelocity, 0);
}
private void Deaccelerate(float _deaccelerationMultiplier)
{
float _currentSpeed = _playerRigidbody.velocity.magnitude;
float _newSpeed = _currentSpeed - _deaccelerationMultiplier;
if (_newSpeed < 0) _newSpeed = 0;
_playerRigidbody.velocity = _playerRigidbody.velocity.normalized * _newSpeed;
}
private void Jump(float _jumpMultiplier, ForceMode _forceMode)
{
Vector2 _direction = Vector2.up;
Vector2 _multiplyDirection = _direction * _jumpMultiplier;
_playerRigidbody.AddForce(_multiplyDirection, _forceMode);
}
}
Keep in mind that FixedUpdate() can happen a few times within a single frame. Check if _spacePressed == true in the beginning of your Process_Jumping().

How can I be able to change the object starting position but also to update it's position when moving?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToTarget : MonoBehaviour
{
public enum TransitionState
{
None,
MovingTowards,
Transferring
}
public Transform destinationTransform;
public Transform navi;
public Transform player;
public bool isChild = false;
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public float duration = 10.0f;
public bool go = false;
private float t;
private Transform originTransform;
private float timer;
private TransitionState state = TransitionState.MovingTowards;
private Vector3 originPosition;
void Start()
{
t = 0.0f;
curve.postWrapMode = WrapMode.Once;
originPosition = transform.position;
}
void Update()
{
if (go)
{
switch (state)
{
case TransitionState.MovingTowards:
var v = destinationTransform.position - transform.position;
if (v.magnitude < 0.001f)
{
state = TransitionState.Transferring;
originTransform = destinationTransform;
timer = 0;
return;
}
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(originPosition,
destinationTransform.position, curve.Evaluate(s));
break;
case TransitionState.Transferring:
timer += Time.deltaTime;
this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
if (timer >= 1.0f)
{
this.transform.parent = destinationTransform;
transform.localPosition = new Vector3(0, 0, 0);
isChild = true;
state = TransitionState.None;
this.enabled = false;
return;
}
break;
default:
this.enabled = false;
return;
}
}
}
}
I want to set the starting position before running the game in the editor when I drag the transform to some position when running the game it's changing back to the originPosition if I mark not to use the line at the Start()
//originPosition = transform.position;
Than it will start from the other side at all.
I want it to start moving from where I set it at the start position.
Moving the line
originPosition = transform.position;
To the Update() is also not a solution.
If transform.position returns something different from what you’ve set in edit mode, it’s affected by something else on Start() or Awake() in some script. Also it is possible, that some script sets public go variable to true on awake.

How can I switch between two cameras after a rotation of an object has finished?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationPlay : MonoBehaviour
{
public GameObject head;
public GameObject[] cameras;
private Animator anim;
private bool started = true;
private float animationLenth;
private bool rotateHead = false;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
if(started == true)
{
anim.enabled = true;
anim.Play("New State", 0, 0);
animationLenth = anim.GetCurrentAnimatorStateInfo(0).length;
StartCoroutine(AnimationEnded());
started = false;
}
if (rotateHead == true)
{
cameras[0].SetActive(false);
cameras[1].SetActive(true);
anim.enabled = false;
head.transform.localRotation = Quaternion.Lerp(head.transform.localRotation, Quaternion.Euler(head.transform.localRotation.x, head.transform.localRotation.y, 0f), 1.0f * Time.deltaTime);
}
}
IEnumerator AnimationEnded()
{
yield return new WaitForSeconds(animationLenth);
anim.enabled = false;
rotateHead = true;
}
}
The problem is in this part :
if (rotateHead == true)
{
cameras[0].SetActive(false);
cameras[1].SetActive(true);
anim.enabled = false;
head.transform.localRotation = Quaternion.Lerp(head.transform.localRotation, Quaternion.Euler(head.transform.localRotation.x, head.transform.localRotation.y, 0f), 1.0f * Time.deltaTime);
}
I'm making the cameras switching before the rotation even start, what I need is somehow to make the camera switching when the rotation is end. Not after the rotation start but when the rotation is end.
Since you know what rotation you want to finish at, you can change cameras once you have reached that rotation.
Create and initialize a variable outside of the Update method:
private bool isFinishedTurning = false;
Then check whether the turn is completed in your update method and change cameras if it is complete:
if (rotateHead == true) {
Quaternion targetRotation = Quaternion.Euler(head.transform.localRotation.x, head.transform.localRotation.y, 0f);
head.transform.localRotation = Quaternion.Lerp(head.transform.localRotation, targetRotation, 1.0f * Time.deltaTime);
// Check whether the current localRotation is the same as the target rotation
if (head.transform.localRotation == targetRotation) {
isFinishedTurning = true;
}
}
// If the rotation is complete, swap cameras and disable animation
if (isFinishedTurning == true) {
cameras[0].SetActive(false);
cameras[1].SetActive(true);
anim.enabled = false;
isFinishedTurning = false;
}

Unity Input triggered twice

I created a small 2D sidescroller movement.
private Rigidbody2D rigid;
private BoxCollider2D playerCollider;
private bool isMovingRight = false;
private Vector2 movement;
private bool jumpPressed;
private const int MOVEMENT_SPEED = 5;
private const int JUMP_POWER = 5;
private const float GROUNDCHECK_TOLERANCE_SIDE = 0.05f;
private const float GROUNDCHECK_TOLERANCE_BOTTOM = 0.05f;
private void Start()
{
rigid = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
SetMovement();
}
private void FixedUpdate()
{
Move();
}
private void SetMovement()
{
float horizontalMovement = Input.GetAxis("horizontal") * MOVEMENT_SPEED;
if (Input.GetButtonDown("Jump"))
{
jumpPressed = true;
}
movement = new Vector2(horizontalMovement, rigid.velocity.y);
}
private void Move()
{
if (GroundCheck(true) || GroundCheck(false))
{
if (jumpPressed)
{
movement.y = JUMP_POWER;
jumpPressed = false;
}
}
rigid.velocity = movement;
}
private bool GroundCheck(bool checkLeftSide)
{
Bounds colliderBounds = playerCollider.bounds;
Vector2 rayPosition = colliderBounds.center;
float horizontalRayPosition = colliderBounds.extents.x + GROUNDCHECK_TOLERANCE_SIDE;
if (checkLeftSide)
{
rayPosition.x -= horizontalRayPosition;
}
else
{
rayPosition.x += horizontalRayPosition;
}
return Physics2D.Raycast(rayPosition, Vector2.down, (playerCollider.size.y / 2) + GROUNDCHECK_TOLERANCE_BOTTOM);
}
I register Inputs in Update and handle the Physics in FixedUpdate. When pressing the Jump Button the players jump works fine.
But when pressing jump multiple times, the player jumps up in the air, comes down and jumps one time again.
So if pressing the button more than 1 time the player will jump a second time after finishing the first jump.
How can I avoid this behaviour?
Using your code, I added 2 comments and an else statement to clarify what I mean in my comments. This should fix your issue.
private void SetMovement()
{
float horizontalMovement = Input.GetAxis("horizontal") * MOVEMENT_SPEED;
if (Input.GetButtonDown("Jump"))
{
// Only ever gets set to false if you are grounded and it makes you jump.
// Should only be set to true if you can jump. So you could add your groundChecks here...
// or use the method I am showing
jumpPressed = true;
}
movement = new Vector2(horizontalMovement, rigid.velocity.y);
}
private void Move()
{
if (GroundCheck(true) || GroundCheck(false))
{
if (jumpPressed)
{
movement.y = JUMP_POWER;
// This was the only place this every was set to false, and
// specific conditions were required to make this happen.
jumpPressed = false;
}
}
// fastest way to test
else
{
// yes they pressed jump but they can't jump so reset it, now
// they wont jump as soon as they land. just because they got
// trigger happy.
jumpPressed = false;
}
rigid.velocity = movement;
}

Categories