Reputation: 225
I am trying to make character movement automatically but it freezes when the character goes left or right.
The video:
https://www.youtube.com/watch?v=Ah4cYdXN8Y8
Character Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed;
public float moveDirection;
public float jumpForce;
public AudioSource audioSource;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isJumping = false;
private string direction = "right";
// Start is called before the first frame update
void Start()
{
// Screen.SetResolution(640, 480, true);
}
// Update is called once per frame
void Update()
{
ProcessInputs();
// FlipCharacter();
if(direction == "right"){
MoveRight();
}
if(direction == "left"){
MoveLeft();
}
}
private void FixedUpdate()
{
// Move();
}
private void OnCollisionEnter2D(Collision2D c)
{
if(c.gameObject.tag == "Rightwall"){
Debug.Log("wall");
rb.velocity = new Vector2(-1 * moveDirection * moveSpeed, rb.velocity.y);
direction = "left";
}
if (c.gameObject.tag == "Leftwall")
{
direction = "right";
}
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void MoveRight()
{
rb.velocity = new Vector2(moveDirection * moveSpeed, rb.velocity.y);
if(isJumping){
Debug.Log("yep");
rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Impulse);
audioSource.Play();
}
isJumping = false;
}
private void MoveLeft()
{
rb.velocity = new Vector2(-1* moveDirection * moveSpeed, rb.velocity.y);
if (isJumping)
{
Debug.Log("yep");
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
audioSource.Play();
}
isJumping = false;
}
private void ProcessInputs()
{
moveSpeed = moveSpeed;
if(Input.GetButtonDown("Jump")){
isJumping = true;
// Debug.Log("jump");
}
}
private void FlipCharacter(){
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
Character in the game is scaled 6 times bigger.
As a result, how can I solve my problem? Where am I wrong?
I worked hard to solve my problem but I could not solve it.
How can I solve this lag problem? I am newbie to Unity.
EDIT: I mean character does not move continuously. It moves discretely. My purpose is to make movement more smoothly. In computer programming, I know it is impossible to make movement %100 smoothly, nevertheless, users should not notice "discrete" movement. Movement should seem more realistic.
I do not know the real problem. This may be an FPS problem(Unity problem & Display problem) or I did something wrong in the script. I am a newbie to Unity.
Finally, how can I solve my problem?
EDIT2:
This is my FPS. Are the values normal or abnormal? or Can we say that "If FPS is lower than 60 in some moments, performance of this game is really bad. FPS should be always equal or greater than 60 to say that its performance is good" What do you think?
Upvotes: 3
Views: 476
Reputation: 121
As mentioned in Harry's answer, it might be a problem that you are applying the force in the framerate dependent Update()
function, you could try moving this to FixedUpdate()
.
Otherwise, you can try enabling the Rigidbody's interpolation to hide the jerky movement.
You can do this either in the inspector (see https://docs.unity3d.com/Manual/class-Rigidbody.html) or by code, using rb.interpolation
(see https://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html).
(Additionally, the Editor can sometimes show stuttering that is not present in standalone builds, but for me this only happens in larger projects and not at such a high frequency.)
Upvotes: 0
Reputation: 323
Harry gave an excelent answer here. Besides his hints, i'll recommend you to do this:
Be aware that we just gave the instruction that player's rigidbody have no friction with other collider so it will never stop without a user input. But looking at your code and video it doesn't seen to be a problem
Upvotes: 2
Reputation: 644
Firstly, put movement code inside FixedUpdate if that doesn't work continue reading...
The lag like the movement is likely not from actual lag due to this seeming to be a simple unity project but first, check the fps by checking stats and check unity's profiler if you want to be sure and find out how to optimise the game.
Find out if the rigidbody has any drag and set it to 0 and test if there is still discrete movement.
Next check all types of movement in unity, there are three ways to move an object without having it clip through walls:
The first one is by setting velocity which is the one you chose.
The second one is by using AddForce which in my experience have not found any lag issues with.
So replace rb.velocity = new Vector2(-1* moveDirection * moveSpeed, rb.velocity.y);
with rb.AddForce(moveDirection*moveSpeed);
.
Test it.
The third is to use MovePosition which I do not recommend but you should test if you like this style of movement.
So replace rb.velocity = new Vector2(-1* moveDirection * moveSpeed, rb.velocity.y);
with rb.MovePosition(transform.position+(moveDirection*moveSpeed*Time.deltaTime));
.
Test it.
Here is a video about Unity movement on Youtube: https://www.youtube.com/watch?v=ixM2W2tPn6c
If this all doesn't work it is likely to do with Unity's framerate/screen refresh rate or perhaps even our eyes playing tricks on us.
Upvotes: 3