- Home /
Adding a value to transform.position.y runs very untrusted
Hey guys,
I have a player in my small game that automatically moves from right to left on a platform. The goal is to get as far down as possible without running into the spikes on the sides.
To get further down I solved it like this: If the player stands on a platform he gets the bool "isgrounded", so he can't jump through and is faster than others.
Jumping does the player as in my script to read so:
private void JumpDown()
{
if(canMove == true)
{
if (Input.GetButtonDown("Drop") && isGrounded)
{
transform.position = new Vector3(transform.position.x, transform.position.y + jumpDownValue, transform.position.z);
GetComponent<AudioSource>().Play();
Debug.Log(jumpDownValue);
}
}
}
The problem now is:
Every time I play transform.position.y has a minimal slightly different value.
Also, from the 10th platform on, it suddenly adds less than the specified -0.5f (jumpDownValue).
From time to time there is also a stuttering of the character. He is moved there also only with a smaller value downward, slides then however directly down on the soil. (Not nice for the eye.)
The problem also occurs only from the 10th platform. (I created a video that shows my problem). https://youtu.be/Nwg_JCn7xqw
Did I miss something here or am I doing something completely wrong?
Since I am not yet so far advanced, I lack here the certain know-how to come myself on a solution.
Here is my PlayerScript:
public class PlayerController : MonoBehaviour
{
private Transform playerTransform;
public GameObject player;
public static GameObject playerInstance;
public static SpriteRenderer playerSprite;
public static float movementSpeed = 1f;
public static float jumpDownValue = -0.5f;
private float playerXReset = 0.0f;
private float playerYReset = 0.241f;
private float playerZReset = 0.0f;
public static bool canMove = true;
public bool isFacingRight = true;
public static bool isGrounded;
public static Animator animator;
public static RuntimeAnimatorController animationController;
public AudioClip jumpSound;
private CinemachineVirtualCamera myCinemachine;
public bool isPlayerSpawned = false;
private void Awake()
{
animationController = GameObject.Find("Player").GetComponent<Animator>().runtimeAnimatorController = GameManager.equippedSkin;
}
private void Start()
{
myCinemachine = GameObject.Find("Camera").GetComponent<CinemachineVirtualCamera>();
myCinemachine.Follow = GameObject.FindGameObjectWithTag("Player").transform;
player = GameObject.Find("Player");
canMove = true;
playerSprite = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
GetComponent<AudioSource>().playOnAwake = false;
GetComponent<AudioSource>().clip = jumpSound;
}
private void Update()
{
FaceDirection();
JumpDown();
AddMoreMovementSpeed();
}
public void ResetPlayerPosition()
{
playerTransform = GetComponent<Transform>();
playerTransform.position = new Vector3(playerXReset, playerYReset, playerZReset);
}
private void FaceDirection()
{
if(canMove == true)
{
if (isFacingRight)
{
transform.Translate(Vector3.right * (Time.deltaTime * movementSpeed));
animator.SetBool("isRunning", true);
animator.SetBool("isIdle", false);
playerSprite.flipX = false;
}
else if (!isFacingRight)
{
transform.Translate(Vector3.left * (Time.deltaTime * movementSpeed));
animator.SetBool("isRunning", true);
animator.SetBool("isIdle", false);
playerSprite.flipX = true;
}
}
}
private void JumpDown()
{
if(canMove == true)
{
if (Input.GetButtonDown("Drop") && isGrounded)
{
transform.position = new Vector3(transform.position.x, transform.position.y + jumpDownValue, transform.position.z);
GetComponent<AudioSource>().Play();
Debug.Log(jumpDownValue);
}
}
}
private void OnTriggerEnter2D(Collider2D canJump)
{
if (canJump.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
private void AddMoreMovementSpeed()
{
if(canMove == true)
{
if (LevelManager.reachedPlatformsInOneRun >= 10) { movementSpeed = 1.25f; }
if (LevelManager.reachedPlatformsInOneRun >= 25) { movementSpeed = 1.50f; }
if (LevelManager.reachedPlatformsInOneRun >= 50) { movementSpeed = 1.75f; }
if (LevelManager.reachedPlatformsInOneRun >= 75) { movementSpeed = 2.00f; }
if (LevelManager.reachedPlatformsInOneRun >= 100) { movementSpeed = 2.50f; }
if (LevelManager.reachedPlatformsInOneRun >= 150) { movementSpeed = 3.00f; }
if (LevelManager.reachedPlatformsInOneRun >= 200) { movementSpeed = 3.50f; }
if (LevelManager.reachedPlatformsInOneRun >= 300) { movementSpeed = 4.00f; }
if (LevelManager.reachedPlatformsInOneRun >= 500) { movementSpeed = 5.00f; }
}
}
}
Already times in advance I would like to thank you very much for your help. :)