Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xKarToSx · Sep 22, 2021 at 02:58 PM · transform2d gamepositionunity 2dy

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

222 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# move y position of object not working 2 Answers

How to Position the object perfectly with the Screen Bounds 0 Answers

Create an animation with variables? 1 Answer

How to decide an object's position when using Instantiate? 1 Answer

Updating localPosition using parents' position 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges