Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unexpected behaviour of rb.velocity on a Cubes Movement.

Discussion in 'Physics' started by Kakashi15, Sep 6, 2018.

  1. Kakashi15

    Kakashi15

    Joined:
    Jan 13, 2017
    Posts:
    1
    I'll get right to the problem, I am moving a Cube on a platform which is made of small cube using queue to make a procedural infinity moving platform and i am using rb.velocity to move a player (also a cube) over these cubes/platform but the cube sometime start rotating, i want it slide smoothly and not rotate, i applied a physics material on both player and platform cube for zero friction but it didn't work.

    Video Demo


    Pictures:
    https://drive.google.com/open?id=1QC7dLNpWF8oJ-MyUCVKy_LQLbnCGzX_e
    https://drive.google.com/open?id=1UCmry-W6LuApVNUitu0qEj7OAl9RebJY
    https://drive.google.com/open?id=1t1ZbUExrbRkRnFUNgJ-8Y01V96uddMtB
    https://drive.google.com/open?id=1rl0nA2ckoT6YR3lBJF20_xhDACfaSUmg

    PlayerMovement Script:
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         //Movement
    4.         rb.velocity = new Vector3(forceSpeed, rb.velocity.y, 0);
    5.      
    6.         //CheckifGrounded
    7.         CheckIfGrounded();
    8.  
    9.         //Jump;
    10.         if (Grounded && jump)
    11.         {
    12.             rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
    13.             Grounded = false;
    14.         }
    15.      
    16.     }
    17.     void Update()
    18.     {
    19.         //DistanceTravelled
    20.         distanceTravelled = transform.localPosition.x;
    21.  
    22.         //TouchControls
    23.         if (CrossPlatformInputManager.GetButtonDown("Jump"))
    24.         {
    25.             jump = true;
    26.         }
    27.         else
    28.         {
    29.             jump = false;
    30.         }
    31.  
    32.         //Resize.Shrink
    33.         if (CrossPlatformInputManager.GetButtonDown("Shrink"))
    34.         {
    35.             if (!shrink)
    36.             {
    37.                 shrink = true;
    38.                 //transform.localScale = Vector3.one * 0.5f;
    39.                 Invoke("Reverse", timer);
    40.             }
    41.         }
    42.         anim.SetBool("Shrink", shrink);
    43.  
    44.         //Velocity Check
    45.         speed = rb.velocity.magnitude;
    46.  
    47.         if (speed <= 0.1f)
    48.         {
    49.             FindObjectOfType<Game>().EndGame();
    50.          
    51.         }
    52.  
    53.         //LightMode
    54.         if(distanceTravelled > 100)
    55.         {
    56.             light.enabled = false;
    57.             n++;
    58.         }
    59.  
    60.     }
    61.  
    62. void CheckIfGrounded()
    63.     {
    64.         rayDown = new Ray(new Vector3(transform.position.x, transform.position.y - transform.localScale.y / 2 + 0.1f, transform.position.z), Vector3.down);
    65.  
    66.         RaycastHit hit;
    67.  
    68.         //Bottom
    69.         if(Physics.Raycast(rayDown, out hit))
    70.         {
    71.             Vector3 distance = rayDown.origin - hit.point;
    72.             if(distance.y <= rayLength)
    73.             {
    74.                 Debug.DrawLine(rayDown.origin, hit.point,Color.red);
    75.                 Grounded = true;
    76.             }
    77.             else
    78.             {
    79.                 Debug.DrawLine(rayDown.origin, hit.point,Color.blue);
    80.                 Grounded = false;
    81.             }
    82.         }
    83.      
    84.     }
    PlatfromSpawning Script:
    Code (CSharp):
    1. void Start () {
    2.         platformQ = new Queue<GameObject>();
    3.         for(int i=0;i<numberofObjects;i++)
    4.         {
    5.             platformQ.Enqueue(Instantiate<GameObject>(prefab));
    6.         }
    7.         nextPosition = startPosition;
    8.         for(int i=0;i<numberofObjects;i++)
    9.         {
    10.             Recycle();
    11.         }
    12.     }
    13.  
    14.  
    15.     void Update () {
    16.         if(PlayerMovement.distanceTravelled > platformQ.Peek().transform.position.x + recycleOffset)
    17.         {
    18.             Recycle();
    19.         }
    20.  
    21.         if (PlayerMovement.distanceTravelled > 150)
    22.         {
    23.             minGap.x = 4;
    24.             maxGap.x = 10;
    25.         }
    26.  
    27.         if (PlayerMovement.distanceTravelled > 250)
    28.         {
    29.             maxGap.x = 6;
    30.         }
    31.         if (PlayerMovement.distanceTravelled > 300)
    32.         {
    33.             maxGap.x = 5;
    34.             minGap.x = 2;
    35.         }
    36.     }
    37.  
    38.     void Recycle()
    39.     {
    40.         Vector3 scale = new Vector3(Random.Range(minSize.x, maxSize.x), Random.Range(minSize.y, maxSize.y), Random.Range(minSize.z, maxSize.z));
    41.  
    42.         Vector3 position = nextPosition;
    43.         position.x += scale.x * 0.5f;
    44.         position.x += scale.x * 0.5f;
    45.  
    46.         GameObject o = platformQ.Dequeue();
    47.         o.transform.localPosition = nextPosition;    
    48.         platformQ.Enqueue(o);
    49.  
    50.         nextPosition.x += Random.Range(minGap.x, maxGap.x);
    51.         nextPosition.y = Random.Range(minGap.y, maxGap.y);
    52.  
    53.     }
    54. }
     
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207