Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rigidbody.MovePosition not colliding properly

Discussion in 'Physics' started by VXPuddiM, Dec 15, 2017.

  1. VXPuddiM

    VXPuddiM

    Joined:
    Jan 23, 2017
    Posts:
    10
    Hey,
    I have a capsule collider with a rigidbody and this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMoveNew : MonoBehaviour {
    6.  
    7.     Rigidbody rb;
    8.     public float speed;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate ()
    18.     {
    19.         float h = Input.GetAxisRaw("Horizontal");
    20.         float v = Input.GetAxisRaw("Vertical");
    21.  
    22.         Vector3 moveFor = v * transform.forward * speed;
    23.         Vector3 moveSide = h * transform.right * speed;
    24.  
    25.         rb.MovePosition(rb.position + (moveFor + moveSide) * Time.deltaTime);
    26.     }
    27. }
    28.  
    It kinda works, but it whenever it collides with something and the player keeps holding the key down, it will enter the object a little bit.

    I've been trying to fix this problem for like 2 hours, but I can't find a solution to it. Some answers I found in the internet said that I should use AddForce but it would keep increasing speed, and that's not what I want. Others said i should set Interpolate to Interpolation, but it doesn't works too.
     
  2. KeithKong

    KeithKong

    Joined:
    May 31, 2015
    Posts:
    73
    You need to use the AddForce functionality if your rigidbody is non-kinematic. Yes, continually adding force will increase velocity. But you can do something like:

    rb.AddForce(desiredVelocity - rb.velocity, ForceMode.VelocityChange);

    and mimic the linear MovePosition behavior using forces (so collisions can work properly).
     
    nuriidyliz likes this.
  3. VXPuddiM

    VXPuddiM

    Joined:
    Jan 23, 2017
    Posts:
    10
    The movement part seems to work, but gravity doesn't.
    I changed this
    Code (CSharp):
    1.         Vector3 moveFor = v * transform.forward * speed;
    2.         Vector3 moveSide = h * transform.right * speed;
    3.  
    4.  
    5.         rb.MovePosition(rb.position + (moveFor + moveSide) * Time.deltaTime);
    for this
    Code (CSharp):
    1.         Vector3 moveFor = v * transform.forward;
    2.         Vector3 moveSide = h * transform.right;
    3.  
    4.  
    5.         rb.AddForce(((moveFor + moveSide).normalized * speed) - rb.velocity, ForceMode.VelocityChange);
     
  4. nuriidyliz

    nuriidyliz

    Joined:
    Jul 6, 2017
    Posts:
    1
    I've been looking for a solution like this for hours, couldn't find a way that the addForce method could mimic MovePosition, thank you very much