Issue with Rigidbody2D (I think)
There is my problem : i have 1 little ghost who have à velocity of 5 and 1 game object with the Camera with a speed of 5 to but when the ghost is on the ground he is slower than the Camera and i can't explain it?
Script of the Ghost:
public class PlayerController : MonoBehaviour {
Vector2 velocity = Vector2.zero;
public float _jump = 10f;
private bool isGrounded = false;
public static float _velocityX;
int touch;
public float start;
public Transform groundCheck;
public float groundCheckSize;
public LayerMask ItsGround;
// Use this for initialization
void Start () {
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckSize, ItsGround);
}
// Update is called once per frame
void Update () {
start = Timer.t;
if (start < 0)
{
_velocityX = 0f;
}
else {
_velocityX = 5f;
GetComponent<Rigidbody2D>().velocity = new Vector2(_velocityX, 0);
if (Input.touchCount >= 1 && isGrounded == true)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(_velocityX, _jump);
Debug.Log(Input.GetTouch(0).position);
}
_velocityX = _velocityX * 1.01f;
}
}
Script of the Camera: public class Speed : MonoBehaviour { // Use this for initialization private float velocityX; void Start () {
}
// Update is called once per frame
void Update () {
velocityX = PlayerController._velocityX;
GetComponent<Rigidbody2D>().velocity = new Vector2(velocityX, 0);
}
}
Thank you.
Answer by hexagonius · Jan 22, 2017 at 06:27 PM
I think it's because the ghost touches the ground and therefore experiences fiction. add a PhysicsMaterial2D to it's collider and make it ice.
also don't update the velocity in Update but in FixedUpdate. that's because it's a value that should only be changes in the Physics Update Loop
Your answer
![](https://koobas.hobune.stream/wayback/20220612105342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Partial synchronization of camera with the bone of the head or other object 0 Answers
Multiplayer Question 1 Answer
Player Movement with Camera Script 1 Answer
Sit Down First person 0 Answers
Face direction of a Vector 3 1 Answer