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 Alaeddina · Jun 18, 2020 at 10:32 AM · flickeringx-axisboundarylimitationsborders

object in Jump&Run-Game is flickering when hitting boundaries - how to avoid?

I'm trying to create a 3D-Jump&Run-Game but I have this issue with the flickering. My assumption was that since the player's pivot is clamped to the borders, half of the player leaves the boundaries (reason for flickering).

So I did this (but unfortunately it is not working) :

public class PlayerController : MonoBehaviour {

 Rigidbody myRigidbody;
 public float speed = 6f;
 

 /* variables to limit movement range in x-axis
  * border values determined by visual estimate in scene view
  * issue: the player's pivot is clamped to the borders --> half of the player leaves the border --> 
     flickering
  * modify border values and add/substract half of the player's size (done in RunHandler())
  */

 float objectWidth;
 float minX = -1.601f;
 float maxX = 1.671f;

 void Start()
 {
     //compute player's size and devide it by 2 --> for keeping gameobject within boundaries
     objectWidth = transform.GetComponent<MeshRenderer>().bounds.size.x / 2;

     myRigidbody = GetComponent<Rigidbody>();
 }


 void FixedUpdate()
 {
     RunHandler();
 }

 void RunHandler()
 {
     float moveVertical = Input.GetAxis("Vertical");
     float moveHorizontal = Input.GetAxis("Horizontal");

     Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
     myRigidbody.MovePosition(transform.position + (movement * speed));
     
     /*
      * set a variable to the position
      * modify the variable to keep x within minX and maxX
      * set the transform position to the modified vector
     */
     Vector3 currentPosition = transform.position;
     currentPosition.x = Mathf.Clamp(currentPosition.x, minX + objectWidth, maxX - objectWidth);
     transform.position = currentPosition;
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LeFlop2001 · Jun 18, 2020 at 10:59 PM

Why not just use Colliders as boundaries

Comment
Add comment · Show 1 · Share
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
avatar image Alaeddina · Jun 20, 2020 at 12:18 PM 0
Share

Dear @LeFlop2001 thank you very much for your response and your help!!

I know, I could create invisible walls to solve this issue but I'd like to programm it.. There has to be a possible way similar to the execution of keeping the player within screen boundaries.

Thanks again :) and have a great weekend

avatar image
0

Answer by tadadosi · Jun 20, 2020 at 02:34 PM

When you are dealing with physics using a rigidbody, never use transform.position unless you set your rigidbody to kinematic and zero out its velocity.

To fix your problem you need to change all your transform.position to myRigidbody.position.

Comment
Add comment · Show 1 · Share
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
avatar image tadadosi · Jun 21, 2020 at 12:05 PM 0
Share

@Alaeddina Where you able to solve your problem?

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

128 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

Related Questions

Restric the script and movement in specific area 0 Answers

Restrict camera rotation in x and y? 1 Answer

(mesh related) code that works in Unity 3.5 does not work in 4.3 1 Answer

How to render black borders around texture of simple 3D quad in c#? 0 Answers

Are there limitations to the Pro Trial? 1 Answer


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