- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
carpediem23 · Sep 08, 2012 at 03:02 AM ·
coroutinejumpmove
Jump problem help
i ve problem with jump - jumpaction - checkischaracteronair numerators and methods so when my player falling down the first y position diffrent with current position after falling i cant understand why i cant falling first position exapmle my _characterGround value is 10.08, after falling current position is 10.07 or 10.06 is it a bug or my wrong ??? ty for help
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
sealed class CharacterBehaviour : Character, IMove
{
#region Player's Status
//This enum define _player's status
private enum Status
{
Move, Jump, Wait, Run, ChangePosition
}
#endregion
#region Player's Land Status
//This enum define _player's status
private enum LandStatus
{
OnCenter, OnRight, OnLeft, OnMove
}
#endregion
#region Wanted Direction
//This enum define user's wanted direction
private enum Directions
{
Left, Right
}
#endregion
#region Public Variables
public int myPlayerSpeed;
public Transform playersLeft;
public Transform playersRight;
public Transform playersCenter;
public float speedFactor;
public GameObject player;
public float transitionDuration;
public const string playersName = "Player";
public static int a = 5;
public bool canPlayerGo;
public List<BonusInfo> bonusList; //Storage bonus infos
public bool canJump;
public float jumpSpeed;
public float maxJumpMeter;
#endregion
#region Private Variables
private bool _isMove;
private float _gameStartedTime;
private bool _isDamage;
private bool _canMove;
private float _distance;
private float _moveSpeed;
private Transform _targetDirectionTransform;
private GameObject _player;
private Transform _playersTransform;
private Transform _endPointTransform;
private Transform _startPointTransform;
private int _playerStatus;
private int _playersLand;
private Vector3 _playersPosition;
private bool _canChangePosition;
private const int _nullValue = -1;
private bool _isPressedButton;
private bool _isHit;
private float _startPositionValue;
private float _playerDistance; //Player's distance
private int _totalScore;
private bool _onAir;
**private float _characterGroundPosition;**
private const float _approachDistance = 5.0f;
private const float _changeDirectionSpeed = 2.0f;
private const float _approachDistanceForCenter = 0.4f;
#endregion
protected override void Awake()
{
base.Awake();
//Variables Values
bonusList = new List<BonusInfo>();
GameManager.isGameEnded = false;
this.gameObject.transform.name = playersName;
GameManager.playersName = playersName;
_isHit = false;
_canChangePosition = false;
_isPressedButton = false;
_player = this.gameObject;
_playersTransform = _player.transform;
_playersPosition = _playersTransform.position;
_startPointTransform = _playersTransform;
_endPointTransform = GameObject.FindWithTag("End Point").transform;
_canMove = false;
_onAir = false;
_playerStatus = (int)Status.Wait;
Debug.Log("This is ChatacterBehaviour's Awake Method");
}
// Use this for initialization
void Start ()
{
_gameStartedTime = Time.time;
_startPositionValue = player.transform.position.z;
_characterGroundPosition = player.transform.localPosition.y;
**StartCoroutine("CheckValues");**
if(canPlayerGo)
{
StartCoroutine("Move");
}
Debug.Log("My Player's Speed = " + myPlayerSpeed);
}
// Update is called once per frame
void Update ()
{
}
#region ChackValues
//This numerator to do check for is user pressed any key, distance of transforms and speed calculate
public IEnumerator CheckValues()
{
while (true)
{
_distance = Vector3.Distance(_startPointTransform.position, _endPointTransform.position);
//10 meter per second
_moveSpeed = Time.deltaTime * speedFactor;
_playersTransform = _player.transform;
CheckIsCharacterOnAir();
//Check Is user click to Left or Right
CheckUpControls();
yield return null;
}
}
#endregion
#region
//This method to do check is player on air?
**private void CheckIsCharacterOnAir()**
{
if(player.transform.position.y == _characterGroundPosition)
{
_onAir = false;
_playerStatus = (int)Status.Move;
}
else
{
_onAir = true;
_playerStatus = (int)Status.Jump;
}
}
#endregion
#region Chack Up Controls
//This method to do input's action
private void CheckUpControls()
{
if(_canMove)
{
if(Input.GetKeyDown(KeyCode.Space) && canJump)
{
_canChangePosition = false;
StartCoroutine("Jump");
}
if (Input.GetKeyDown(KeyCode.A) && !_onAir)
{
_isPressedButton = true;
_canChangePosition = true;
_targetDirectionTransform = playersLeft;
_playersLand = FindPlayersLand();
Debug.Log("Player's position number is :" + _playersLand);
ChoosePosition(_playersLand, (int)Directions.Left);
Debug.Log("User click LEFT");
}
else if (Input.GetKeyDown(KeyCode.D) && !_onAir)
{
_isPressedButton = true;
_canChangePosition = true;
_targetDirectionTransform = playersRight;
_playersLand = FindPlayersLand();
Debug.Log("Player's position number is :" + _playersLand);
ChoosePosition(_playersLand, (int)Directions.Right);
Debug.Log("User click RIGHT");
}
_isPressedButton = false;
}
}
#endregion
#region Choose Position
//This method to do Where player is
private void ChoosePosition(int paramPositionInfo, int paramWantedDirection)
{
if(_canChangePosition)
{
Debug.Log("PosNumber : " + paramPositionInfo);
if(paramPositionInfo == (int)LandStatus.OnCenter)
{
StartCoroutine(TakeMe(paramWantedDirection, paramPositionInfo));
}
else if(paramPositionInfo == (int)LandStatus.OnLeft && paramWantedDirection != (int)Directions.Left)
{
StartCoroutine(TakeMe(paramWantedDirection, paramPositionInfo));
}
else if(paramPositionInfo == (int)LandStatus.OnRight && paramWantedDirection != (int)Directions.Right)
{
StartCoroutine(TakeMe(paramWantedDirection, paramPositionInfo));
}
else
_canChangePosition = false;
}
}
#endregion
#region Jump
//This numerator to do jump player
**IEnumerator Jump()**
{
if(!_onAir)
{
yield return StartCoroutine("JumpAction");
}
yield return null;
}
#endregion
#region JumpAction
//This numerator to do finish jump action
**IEnumerator JumpAction()**
{
Debug.Log(_characterGroundPosition);
Debug.Log("Player is jumping");
float _tempUpdatedTime;
while(player.transform.position.y <= maxJumpMeter)
{
_tempUpdatedTime = Time.deltaTime * (Time.timeScale / transitionDuration);
player.transform.position += Vector3.up * _tempUpdatedTime * 50f;
yield return null;
}
StartCoroutine("FallPlayer");
yield return null;
}
#endregion
#region FallPlayer
//This numerator to do fall player
**IEnumerator FallPlayer()**
{
Debug.Log("Fall player");
float _tempUpdatedTime;
while(player.transform.position.y >= _characterGroundPosition)
{
_tempUpdatedTime = Time.deltaTime * (Time.timeScale / transitionDuration);
player.transform.position += Vector3.down * _tempUpdatedTime * 52f;
yield return null;
}
Debug.Log(player.transform.position.y);
yield return null;
}
#endregion
#region TakeMe
//This numerator to do changing plyer's direction
public IEnumerator TakeMe(int paramDirection, int paramPositionInfo)
{
if(true)
{
Debug.Log("Take me : " + paramDirection + " " + "direction");
Transform _wantedTransform = null;
Vector3 _tempDirection = Vector3.zero;
if(paramDirection == (int)Directions.Left)
{
if(paramPositionInfo == (int)LandStatus.OnCenter) {_wantedTransform = playersLeft; _tempDirection = Vector3.left;}
else if(paramPositionInfo == (int)LandStatus.OnRight) {_wantedTransform = playersCenter; _tempDirection = Vector3.left;}
}
if(paramDirection == (int)Directions.Right)
{
if(paramPositionInfo == (int)LandStatus.OnCenter) {_wantedTransform = playersRight; _tempDirection = Vector3.right;}
else if(paramPositionInfo == (int)LandStatus.OnLeft) {_wantedTransform = playersCenter; _tempDirection = Vector3.right;}
}
float _tempUpdatedTime = 0.0f;
if(paramPositionInfo == (int)LandStatus.OnLeft || paramPositionInfo == (int)LandStatus.OnRight)
{
_playerStatus = (int)Status.ChangePosition;
canJump = false;
while (Math.Abs(player.transform.localPosition.x - playersCenter.position.x) >= _approachDistanceForCenter)
{
_canChangePosition = false;
_playerStatus = (int)Status.Move;
_tempUpdatedTime = Time.deltaTime * (Time.timeScale / transitionDuration);
player.transform.position +=_tempDirection * _moveSpeed * myPlayerSpeed * _changeDirectionSpeed;
//player.transform.position = Vector3.Lerp(player.transform.position, _wantedTransform.position, _tempUpdatedTime);
//player.transform.Translate(_wantedTransform.position * _tempUpdatedTime);
yield return null;
}
canJump = true;
_playerStatus = (int)Status.Move;
}
else
{
_playerStatus = (int)Status.ChangePosition;
canJump = false;
while (Math.Abs(player.transform.localPosition.x - _wantedTransform.position.x) >= _approachDistance)
{
_canChangePosition = false;
_playerStatus = (int)Status.Move;
_tempUpdatedTime = Time.deltaTime * (Time.timeScale / transitionDuration);
player.transform.position +=_tempDirection * _moveSpeed * myPlayerSpeed * _changeDirectionSpeed;
//player.transform.position = Vector3.Lerp(player.transform.position, _wantedTransform.position, _tempUpdatedTime);
//player.transform.Translate(_wantedTransform.position * _tempUpdatedTime);
yield return null;
}
canJump = true;
_playerStatus = (int)Status.Move;
}
_canChangePosition = true;
}
}
#endregion
#region FindPlayer's Land
//For find where the player is
private int FindPlayersLand()
{
Vector3 _myPosition = player.transform.position;
int _tempPosition = _nullValue;
if(Math.Abs(player.transform.localPosition.x - playersLeft.position.x) <= _approachDistance)//myPosition == playersLeft.position)
{
_tempPosition = (int)LandStatus.OnLeft;
Debug.Log("Player on the left");
}
else if(Math.Abs(player.transform.localPosition.x - playersRight.position.x) <= _approachDistance)
{
_tempPosition = (int)LandStatus.OnRight;
Debug.Log("Player on the right");
}
else if(Math.Abs(player.transform.localPosition.x - playersCenter.position.x) <= _approachDistanceForCenter)
{
_tempPosition = (int)LandStatus.OnCenter;
Debug.Log("Player on the center");
}
//For first time
else if(Math.Abs(Vector3.Distance(_myPosition, playersCenter.position)) == 0.0f)
{
_tempPosition = (int)LandStatus.OnCenter;
Debug.Log("Player on the center");
}
else
{
_tempPosition = (int)LandStatus.OnMove;
Debug.Log("Player on the moving");
}
return _tempPosition;
}
#endregion
#region Move IENumetator
// This numerator to do controll other numerators about moving
public IEnumerator Move()
{
Debug.Log("Inside Move Numerator");
yield return new WaitForSeconds(GameManager.remainingTime);
_playerStatus = (int)Status.Move;
_canMove = true;
_canChangePosition = true;
Debug.Log("Move is started");
StartCoroutine("MoveToSingle");
StopCoroutine("Move");
}
#endregion
#region MoveTo
// This numerator to do take player from start position to end position
public IEnumerator MoveToSingle()
{
if(_canMove)
{
while (_distance > 1)
{
this.transform.position += Vector3.forward * _moveSpeed * myPlayerSpeed;
_playerDistance = GetMyDistance();
yield return null;
}
_canMove = false;
GameManager.FinishToGame();
StopAllCoroutines();
}
}
#endregion
#region GetMyDistance
//This method to do check player's distance from start to end points
private float GetMyDistance()
{
float _result;
_result = Math.Abs(player.transform.position.z - _startPositionValue);
return _result;
}
#endregion
#region CalculateScore
//This method to do calculate user score
public void CalculateScore()
{
Debug.Log("Bonus List Count : " + bonusList.Count.ToString());
for(int i = 0; i < bonusList.Count; i++)
{
Debug.Log("---------Bonus List--------");
Debug.Log("Name: " + bonusList[i].name);
Debug.Log("Score Value: " + bonusList[i].scoreValue);
Debug.Log("Each: " + bonusList[i].each);
Debug.Log("---------------------------");
}
}
#endregion
}
Comment
Your answer

Follow this Question
Related Questions
Can't Modify PlatformerCharacter2D Jump Force From Other Script 0 Answers
Move camera on the z axis forward and backward with coroutine 0 Answers
movement doesnt stop when jump pad is tapped at the same time 0 Answers
How to stop coroutines or functions 2 Answers
CharacterController acting up when jumping and moving 1 Answer