- Home /
Rigidbody2D interpolate setting lets player fly
Hey guys. Yesterday i made a thread where i asked how i could fix the problem with my jittering. I saw many answers on other threads that you should turn on the interpolate setting. Well for me the interpolate setting does this (video attached) link text This is my movementScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed = 1;
public float JumpForce = 1;
private Rigidbody2D _rigidbody;
// Start is called before the first frame update
private void Start()
{
_rigidbody = GetComponent < Rigidbody2D >();
}
// Update is called once per frame
private void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * movementSpeed;
if(Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f){
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
}
And this is my CameraFollowScript (you probably wont need that):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float smoothFactor;
private void FixedUpdate()
{
Follow();
}
void Follow()
{
Vector3 targetPosition = target.position + offset;
Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition, smoothFactor * Time.fixedDeltaTime);
transform.position = smoothPosition;
}
}
I want to turn on the interpolate setting because this could maybe stop the jittering but as you see in the video it does some weird stuff which is probably related to the movementScript but im still a total beginner and noob thats why i cant see where the problem in the coding is. If someone knows a solution which solves this problem please help me :( (also if you know a solution for the jittering problem in general you are more than welcome to write it in the thread :) )
Answer by Box_Frog · Feb 12, 2021 at 12:52 AM
I had a similar problem before and the issue was the camera. If you disable the camera script is your player still jittery? I was playing around with your camera script, try to multiply your smoothFactor by Time.deltaTime instead of fixedDeltaTime.
public class CameraFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float smoothFactor;
private void Start()
{
offset = transform.position - target.position;
}
private void FixedUpdate()
{
Follow();
}
void Follow()
{
Vector3 targetPosition = target.position + offset;
Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition, smoothFactor * Time.deltaTime);
transform.position = smoothPosition;
}
also in unity if you go to "Window" then "Package manager" there's a really useful package called Cinemachine. Here's a tutorial if you're interested. Tutorial Link
$$anonymous$$y player is still jittering even with Time.deltaTime. And no, my player isnt jittering anymore when i turn off the camera script. I also tried cinemachine, but my player still jittered lol. Imma try changing the sprite maybe that will help?
What do you have your offset or smoothFactor set to? And I forgot to mention that with Cinemachine under your main camera in the CinemachineBrain I change the "Update $$anonymous$$ethod" and "Blend Update $$anonymous$$ethod" to Fixed Update.
$$anonymous$$y offset setting is set to -1 and my smooth factor is set to 3. With cinemachine my character only jitters when jumping. When i do fixedUpdate on both it jitters in all ways again. When i do smart update on Update$$anonymous$$ethod and LateUpdate on BlendUpdate$$anonymous$$ethod its the best so far, because my player is only a bit jittery when in air.
Hmm. you could try and put your player movement ( transform.position += new Vector3(movement, 0, 0) Time.deltaTime movementSpeed; ) in FixedUpdate and then Cinemachine to Update in FixedUpdate.
Dude that fixed it. I put my movement in FixedUpdate and for my jump if statement i made a new script (becuase jumping in FixedUpdate doesnt really work). All jitteriness is gone. Thx a lot!
Your answer
Follow this Question
Related Questions
how to make a 2d character three-dimensional? 1 Answer
Help Me! I want to make a pixel fairy! 1 Answer
2D Dust Particle Trail? 1 Answer