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?
from Character freezes when it moves automatically
No comments:
Post a Comment