Rantiev
Rantiev

Reputation: 2242

Unity 3d - player is moving constantly after touching a wall

After my player object touches the wall it sometimes starts to move and rotate on it's own. I tried to increase player object weight and it helps, but i don't think it's a good approach as tiny movement never dissapears.

Player is rigidbody with box collider attached.

isKinematic - false; useGravity - true; XYZ rotation is fixed and Y coordinate position is fixed.

Walls have box collider but have no rigidbody. Ground has no collider as Y position is fixed and player object doesn't touch the ground.

Game is network based so i have photon rigidbody view component attached to player as well.

Code (C#) which moves player:

public void Update()
{
    if (!photonView.IsMine || !controllable)
    {
        return;
    }

    if (shootingTimer > 0.0)
    {
        shootingTimer -= Time.deltaTime;
    }

    m_MovementInputValue = Input.GetAxis(m_MovementAxisName); 
    if (m_MovementInputValue == 0.0f)
    {
        m_MovementInputValue = joystick.Vertical;
    }

    m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
    if (m_TurnInputValue == 0.0f)
    {
        m_TurnInputValue = joystick.Horizontal;
    }

    Vector3 vector = new Vector3(
        m_TurnInputValue,
        rigidbody.velocity.y,
        m_MovementInputValue
    );

    MovementVector = Quaternion.AngleAxis(60, Vector3.up) * vector;

    EngineAudio();
}

public void FixedUpdate()
{
    if (!photonView.IsMine || !controllable)
    {
        return;
    }

    Move();
    Turn();
}

private void Move()
{
    // Adjust the position of the tank based on the player's input.
    // Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
    // rigidbody.MovePosition(rigidbody.position + movement);

    Vector3 movement = MovementVector * m_Speed * Time.deltaTime;
    rigidbody.MovePosition(rigidbody.position + movement);
}

private void Turn()
{
    // Adjust the rotation of the tank based on the player's input.
    // float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
    // Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
    // rigidbody.MoveRotation(rigidbody.rotation * turnRotation);
    if (m_TurnInputValue != 0.0f || m_MovementInputValue != 0.0f)
    {
        rigidbody.rotation = Quaternion.LookRotation(MovementVector);
    }        
}

Upvotes: 2

Views: 1621

Answers (1)

h4le5torm
h4le5torm

Reputation: 113

Check to make sure the component (such as a rigidbody) is attached to the right object (gameobject - parent vs. child objects). Also, the fact that it is constantly rotating is curious... it makes me think it has something to do with the Time.DeltaTime, but I see that's only being applied to your Vector3 movement, and not in your rotation function... Just curious, what happens when you remove the Time.DeltaTime part from your movement Vector3?

Upvotes: 0

Related Questions