Accelerometer stop animation Movement query - c#

I have a script named AccelerometerMovement which is taking care of accelerometer controls of player. The player is just moving left and right so I am just taking Input.acceleration.x component.
The script is as follows:
public class AccelerometerMovement : MonoBehaviour {
private bool isandroid;
private float AccelerometerStoreValue;
private robotController theRobo;
// Use this for initialization
void Start () {
theRobo = FindObjectOfType<robotController> ();
#if UNITY_ANDROID
isandroid=true;
#else
isandroid=false;
#endif
}
// Update is called once per frame
void Update () {
if (isandroid) {
//android specific code
Accelerometer();
} else {
//any other platform specific code
}
}
void Accelerometer(){
AccelerometerStoreValue = Input.acceleration.x;
if (AccelerometerStoreValue > 0.1f) {
//right
theRobo.moveRight();
} else if (AccelerometerStoreValue < -0.1f) {
//left
theRobo.moveLeft();
}
}
}
As u can see above according to left and right..it is calling moveLeft() and moveRight() from another script which is the actual player controller script.
The other script where the actual function is :
// after Update()
public void jump(){
if (grounded) {
myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, jumpHeight);
doubleJump = false;
}
if(!doubleJump&&!grounded){
myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, jumpHeight);
doubleJump = true;
}
}
public void moveLeft(){
myRigidBody.velocity = new Vector2 (-moveSpeed, myRigidBody.velocity.y);
robotMove = true;
lastMove = myRigidBody.velocity.x;
anim.SetFloat ("MoveX", -moveSpeed);
anim.SetFloat ("LastMoveX", lastMove);
anim.SetBool ("RobotMoving", robotMove);
}
public void moveRight(){
myRigidBody.velocity = new Vector2 (moveSpeed, myRigidBody.velocity.y);
robotMove = true;
lastMove = myRigidBody.velocity.x;
anim.SetFloat ("MoveX", moveSpeed);
anim.SetFloat ("LastMoveX", lastMove);
anim.SetBool ("RobotMoving", robotMove);
}
public void stop(){
robotMove = false;
anim.SetBool ("RobotMoving", robotMove);
}
Now when I checked the controls on actual device the controls are working fine but there is one problem!
The problem is that when player starts moving the animation of movement starts but when it stops the idle animation (or stop animation) doesn't start and even when still the player movement animation keeps on going.
Now I am not able to understand how to solve this problem.

the Stop() function above is lying dormant. We need to call it by putting one extra condition :
else if (AccelerometerStoreValue > 0.01f && AccelerometerStoreValue < 0.09f || AccelerometerStoreValue < -0.01f && AccelerometerStoreValue > -0.09f) {
theRobo.stop ();
}
the values will take care of device still and will set the idle animations to work ! Put above code just after 2nd else if in Accelerometer() function .

Related

How to switch between the OnTriggerExit/Enter logic depending on the situation?

The script is attached to two gameobjects.
One it's colliding area the collider is big enough so when the game start the player is already inside the collider area and then when he exit the area everything is working fine.
The problem is when I attached the object to another gameobject with collider but this time the collider s smaller and he is inside the bigger collider so now the player is entering the smaller collider and not first time exiting. but I want in both case to make the same effect.
If the player exit the collider he slow down wait then turn around and move inside back.
The same I want to make when the player getting closer to the fire flames slow down wait turn around and move back in it's just with the flames the player entering the collider area and then exit while in the bigger collider he first exit then enter.
Screenshot of the two colliders areas. The small one on the left is the one the player entering first and not exiting. The game start when the player is in the bigger collider area.
And the script :
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;
public class DistanceCheck : MonoBehaviour
{
public Transform targetToRotateTowards;
public Transform colliderArea;
public float lerpDuration;
public float rotationSpeed;
[TextArea(1, 2)]
public string textToShow;
public GameObject descriptionTextImage;
public TextMeshProUGUI text;
public ThirdPersonUserControl thirdPersonUserControl;
private Animator anim;
private float timeElapsed = 0;
private float startValue = 1;
private float endValue = 0;
private float valueToLerp = 0;
private bool startRotating = false;
private bool slowOnBack = true;
private bool exited = false;
private Vector3 exitPosition;
private float distance;
void Start()
{
anim = transform.GetComponent<Animator>();
}
private void FixedUpdate()
{
if (startRotating)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation,
Quaternion.LookRotation(targetToRotateTowards.position - transform.position),
rotationSpeed * Time.deltaTime);
}
if (exitPosition != new Vector3(0, 0, 0) && slowOnBack)
{
distance = Vector3.Distance(transform.position, exitPosition);
}
if (distance > 5 && slowOnBack)
{
slowOnBack = false;
StartCoroutine(SlowDown());
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == colliderArea.name)
{
exited = true;
slowOnBack = true;
exitPosition = transform.position;
thirdPersonUserControl.enabled = false;
descriptionTextImage.SetActive(true);
text.text = textToShow;
StartCoroutine(SlowDown());
}
}
private void OnTriggerEnter(Collider other)
{
if (other.name == colliderArea.name)
{
exited = false;
startRotating = false;
text.text = "";
descriptionTextImage.SetActive(false);
}
}
IEnumerator SlowDown()
{
timeElapsed = 0;
while (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
yield return null;
}
if (exited)
{
yield return new WaitForSeconds(3f);
startRotating = true;
StartCoroutine(SpeedUp());
}
if (slowOnBack == false)
{
thirdPersonUserControl.enabled = true;
}
}
IEnumerator SpeedUp()
{
timeElapsed = 0;
while (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(endValue, startValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
yield return null;
}
}
}
Two problems I'm facing right now :
The smaller collider the player enter it then exit and the bigger collider the player exit it then enter so I need to change somehow the OnTriggerExit/Enter behavior logic in the script. but how to make the logic ?
Maybe it's better to make the script to be on the player object only and make it some how generic so I can drag to it many colliders and to make the effect in each one of the colliders the problem is how to make a text field for each collider area ? Now because I attach the script to each collider object I have one colliderArea variable but if I want to make the script only attached to the player I need to change the colliderArea variable to a List<Transform> collidersAreas and then how to create a text field/area to each collider area in the List ?
I think I would do this by creating two tags, NoExit and NoEntry. Once the tags are created you can set the tag on the GameObjects that hold your colliders. Then you can check for tags in the OnTriggerEnter and OnTriggerExit and act accordingly.
I can't tell for sure which functionality you've got written is for which case, or if it's both - everything looks muddled. Ultimately what you should be going for is a function like RepositionPlayer, that moves them back to before they're violating the no entry or no exit rules, and then some kind of OnPlayerRepositioned event that restores their control.
I'll leave it up to you to split out your functionality, but in general I'd look to do something like the following:
private void OnTriggerExit(Collider other)
{
if (other.tag == "NoExit")
{
RepositionPlayer();
}
else if(other.tag == "NoEntry")
{
OnPlayerRepositioned();
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "NoExit")
{
OnPlayerRepositioned();
}
else if(other.tag == "NoEntry")
{
RepositionPlayer();
}
}
And again here it's not clear to me what you're trying to do to reposition the player, but it looks like it's something like:
private void RepositionPlayer()
{
// Stuff that needs to happen to reposition the player
exited = true;
slowOnBack = true;
exitPosition = transform.position;
thirdPersonUserControl.enabled = false;
descriptionTextImage.SetActive(true);
text.text = textToShow;
StartCoroutine(SlowDown());
}
private void OnPlayerRepositioned()
{
// stuff you need to do to clear the "repositioning" status
exited = false;
startRotating = false;
text.text = "";
descriptionTextImage.SetActive(false);
}
Splitting up the logic like this makes it easier to both read and maintain.

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.

Touch screen, how to tweak my code to react to only one finger

I'm trying to add limited touch screen controls to my game for my android phone. I keep seeing all these tutorials but I'm just not getting one of the simple parts, at least i think it's simple. On my phone, when you tap it with your finger, I have a truck that goes left. When I tap it again, I want it to right. Every time you tap the screen, it's supposed to go in the other direction, alternating between left and right. It soooort of works now but not really. When you tap it the first time, it goes left. But in order to make it go right, you have to hold the screen down with one finger and then touch it with the second finger. I just want it to react to only one finger at a time so that you can play it with one hand. So here's my code. What am I doing wrong? How do I fix it? Thank you all in advance.
tried getting rid of the returns. Tried tweaking the existing code and adding other things in there based on other tutorials.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playerController : MonoBehaviour {
private Rigidbody rb;
private Animator anim;
private bool left = false;
private bool right = false;
public static bool detect = false;
public float yspeed; //left or right from camera's vp
public float xSpeed = 55f; //down the road from camera's vp
[HideInInspector]
public bool crash = false;
public int slowSpeed;
private float respawnTimer = 3f;
void Start () {
rb = GetComponent<Rigidbody> ();
anim = GetComponent<Animator> ();
rb.velocity = new Vector3 (xSpeed, rb.velocity.y, rb.velocity.z);
}
void Update () {
Movement ();
Halt ();
}
public void Movement()
{
if (crash) {
left = false;
right = false;
scoreManager.scoreValue = 0;
}
detect = false;
if (Input.touchCount > 0 && !left) {
foreach (Touch touch in Input.touches) {
rb.velocity = new Vector3 (rb.velocity.x, yspeed, 0);
left = true;
right = false;
return;
}
}
if (Input.touchCount > 0 && !right) {
foreach (Touch touch in Input.touches) {
rb.velocity = new Vector3 (rb.velocity.x, -yspeed, 0);
right = true;
left = false;
return;
}
}
}
void OnTriggerEnter (Collider other)
{
if (!crash && other.gameObject.tag == ("wall")) {
crash = true;
anim.SetTrigger ("crash");
}
}
public void Halt()
{
if (crash && slowSpeed > 0) {
rb.velocity = new Vector3 (--slowSpeed, 0, 0);
Invoke ("Restart", respawnTimer);
}
}
public void Restart ()
{
Application.LoadLevel ("scene_01");
}
}
As you press and hold, you will notice that it will swap left & right every frame you're touching the screen. Consider using TouchPhase for more refined input. Try this:
...
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) {
if(right) {
rb.velocity = new Vector3 (rb.velocity.x, yspeed, 0);
left = true;
right = false;
} else if (left) {
rb.velocity = new Vector3 (rb.velocity.x, -yspeed, 0);
right = true;
left = false;
}
}
...
See deployed code on an Android device:
I figured it out and it's so stupid. I just had to update my phone and use fire1 and fire2 as if it were mouse controls, which is what i originally had it as. When I updated my phone, the touchCount arguments didn't react AT ALL to my phone so I tried one of my earlier versions when I was still having it work just for PC. This is the code that works for me now.
if (Input.GetButtonDown ("Fire1") && !left){
rb.velocity = new Vector3 (rb.velocity.x, yspeed, 0);
left = true;
right = false;
return;
}
if (Input.GetButtonDown ("Fire1") && left){
rb.velocity = new Vector3 (rb.velocity.x, -yspeed, 0);
right = true;
left = false;
return;
}

Unity2D: How to make my player invulnerable for X amount of time?

I want to make my player invulnerable from objects hitting him for a few seconds after he resets back to the center of the game, meaning I don't want anything to hurt him and I don't want the player to move for 5 seconds, but i'm not sure of how to do that! I searched it up but results that I found doesn't match up with my code. Anyway this is my playermovement script:
private Animator anim;
public float speed = 15f;
public static Vector3 target;
private bool touched;
private bool canmove;
Vector3 startPosition;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint(mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
var movementDirection = (target - transform.position).normalized;
if (movementDirection.x != 0 || movementDirection.y != 0) {
anim.SetBool ("walking", false);
anim.SetFloat("SpeedX", movementDirection.x);
anim.SetFloat("SpeedY", movementDirection.y);
anim.SetBool ("walking", true);
}
}
void FixedUpdate () {
float LastInputX = transform.position.x - target.x;
float LastInputY = transform.position.y - target.y;
if (touched) {
if (LastInputX != 0 || LastInputY != 0) {
anim.SetBool ("walking", true);
if (LastInputX < 0) {
anim.SetFloat ("LastMoveX", 1f);
} else if (LastInputX > 0) {
anim.SetFloat ("LastMoveX", -1f);
} else {
anim.SetFloat ("LastMoveX", 0f);
}
if (LastInputY > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (LastInputY < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
}else{
touched = false;
anim.SetBool ("walking", false);
}
}
}
And this is my player health script:
public int curHealth;
public int maxHealth = 3;
Vector3 startPosition;
bool invincible = false;
void Start ()
{
curHealth = maxHealth;
startPosition = transform.position;
}
void Update ()
{
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if (curHealth <= 0) {
Die ();
}
}
void Die ()
{
//Restart
Application.LoadLevel (Application.loadedLevel);
}
public void Damage(int dmg)
{
curHealth -= dmg;
Reset();
}
void Reset ()
{
transform.position = startPosition;
PlayerMovement.target = startPosition;
}
}
So to be more simple, I want to make my player invulnerable (once he reset back in the center of the screen) from getting hit from enemies for 5 seconds and people who are playing my player can not move for 5 seconds. Thank you! (My game is a topdown 2d click to move game)
I think this should do the trick. At least point you in the right direction.
Simple explanation, i changed invincible to be a time, a time in the future.
Then we simply add a time in the future to it at some event, and from that event onward to the point in the future we want him invincible.
So at every event where he would take damage we check, is now in the future of invincible or is invincible in the future of now. that is your bool.
DateTime invincible = DateTime.Now;
public void Damage(int dmg)
{
if(invincible <= DateTime.Now)
{
curHealth -= dmg;
Reset();
}
}
void Reset ()
{
transform.position = startPosition;
PlayerMovement.target = startPosition;
invincible = DateTime.Now.AddSeconds(5);
}
if you want a helper method like this
bool IsInvincible(){
return invincible <= DateTime.Now;
}
Then you can change out the if(invincible <= DateTime.Now) with if(IsInvincible()) in your public void Damage(int dmg)
In game development, you have to keep in mind that everything happens within a frame update. I like to use coroutines to implement invincibility frames in Unity. A coroutine is simply a method that runs in parallel to the main update loop and resumes where it left off in the next update.
float dmg = 100.0f;
bool isHitByEnemy = false
bool isInvulnerable = false
void Demage()
{
//some dmg logic
if (isHitByEnemy && isInvulnerable == false)
{
StartCoroutine("OnInvulnerable");
}
}
IEnumerator OnInvulnerable()
{
isInvulnerable = true;
dmg = 0.0f;
yield return new WaitForSeconds(0.5f); //how long player invulnerable
dmg = 100.0f;
isInvulnerable = false;
}

Why can't I jump, or jump well?

Right now I'm not getting much Out of this. Depending on what I do different i either end up with an infinite loop, or poor jumping ability. I used a timer to tick my jumped bool but I was getting a double like jump ability and my ground detection wasn't good enough. Can you See why I can't jump, or jump well?
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour
{
public float speed = 0.05f;
public float jumpVelocity = 0.05f;
public bool onGround = true;
public bool jumped = false;
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update ()
{
//test if player is on the ground
if (onGround) { jumped = false; }
// USER CONTROLS HERE.
if (Input.GetKeyDown(KeyCode.Space) && onGround == true)
{
jumped = true;
while(jumped)
{
this.rigidbody2D.AddForce(new Vector2(0, jumpVelocity));
if (onGround) { jumped = false; }
}
}
else if (Input.GetKey(KeyCode.RightArrow))
{
this.transform.position += Vector3.right * speed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
this.transform.position += Vector3.left * speed * Time.deltaTime;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "floor") { onGround = true; }
}
void OnCollisionStay2D(Collision2D col)
{
if(col.gameObject.tag == "floor") { onGround = true; }
}
}
Your problem stems from a misunderstanding how the Update method and physics work. If you do this in the update method, it will create an endless loop:
while(jumped)
{
this.rigidbody2D.AddForce(new Vector2(0, jumpVelocity));
if(onGround)
{
jumped = false;
}
}
The thing is that you tell the physics body to add a force. But you keep doing so over and over again. The physics simulation only takes place after the Update method returns, so whatever "onGround" is it will never become true because the forces aren't being applied until after the Update method.
Instead you have to make this check over and over again every time the Update method runs until onGround is true.

Categories