Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My cube seem to have "absolut" friction, why?

Discussion in 'Physics' started by joakimweidenius, Oct 16, 2020.

  1. joakimweidenius

    joakimweidenius

    Joined:
    Oct 1, 2020
    Posts:
    1
    I am trying to get a cube to slide when i am going from input to no input. But as it is right now, as soon as i stop pressing "w" it stops directly. I have used the physics material and set both friction to 0. Also the ground (a larger cube) have the same physics material bound to it.

    When i tilt the ground cube the smaller cube slide instantaneously. How can i solve this? The code below is for controlling the cube.

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player1 : MonoBehaviour
    6. {
    7.     private float speed = 10;
    8.     private Vector3 velocity;
    9.  
    10.     Rigidbody playerRigidBody;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         playerRigidBody = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    22.         Vector3 direction = input.normalized;
    23.         velocity = direction * speed;
    24.     }
    25.  
    26.     private void FixedUpdate()
    27.     {
    28.         playerRigidBody.position += velocity * Time.fixedDeltaTime;
    29.     }
    30. }
    31.  
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You need to look at Rigidbody.MovePosition to have it move and stop. You need to add a force Rigidbody.Addforce to start it moving in a given direction and rely on other things (drag, friction, collision) to stop it. Familiarise yourself with:

    f=ma and https://en.wikipedia.org/wiki/Equations_of_motion search for (Constant translational acceleration in a straight line)

    Avoid the temptation to directly set Rigidbody.velocity.

    At the moment you are changing the position without involving the physics engine, so your body is teleporting around. Despite appearances, it has no velocity in the physics engine, so it will not continue to move due to physical effects.

    Be careful of normalising your input vectors without checking their magnitude: Even a tiny input will gain a magnitude of 1.