- Home /
Character moves slower after build
So i am working on the Ruby2D tutorial, and everything goes fine, up until I decide to play the newly built game.
When I try to move my character, it goes extremely slow. This problem however only appears in the build. When i play the Scene everything works well. Changing the speed only has effect in the Scene.
My current controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RubyController : MonoBehaviour
{
public int maxHealth = 5;
public int health { get { return currentHealth; } }
int currentHealth;
public float speed = 13.0f;
public ParticleSystem healthEffect;
public ParticleSystem damageEffect;
public float timeInvincible = 2.0f;
bool isInvincible;
float invincibleTimer;
Rigidbody2D rigidbody2d;
Animator animator;
Vector2 lookDirection = new Vector2(1, 0);
public GameObject projectilePrefab;
AudioSource audioSource;
public AudioClip shotClip;
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
currentHealth = maxHealth;
audioSource = GetComponent<AudioSource>();
}
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector2 move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x,0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
animator.SetFloat("Look X", lookDirection.x);
animator.SetFloat("Look Y", lookDirection.y);
animator.SetFloat("Speed", move.magnitude);
Vector2 position = rigidbody2d.position;
position = position + move * speed * Time.deltaTime;
rigidbody2d.MovePosition(position);
if (isInvincible)
{
invincibleTimer -= Time.deltaTime;
if (invincibleTimer < 0)
isInvincible = false;
}
if (Input.GetKeyDown(KeyCode.C))
{
Launch();
}
if(Input.GetKeyDown(KeyCode.X))
{
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
if (hit.collider != null)
{
NonPlayerCharacter character = hit.collider.GetComponent<NonPlayerCharacter>();
if (character != null)
{
character.DisplayDialog();
}
}
}
}
public void ChangeHealth(int amount)
{
if (amount < 0)
{
if (isInvincible)
return;
ParticleSystem damaged = Instantiate(damageEffect, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
damaged.Play();
isInvincible = true;
invincibleTimer = timeInvincible;
}
else if (amount > 0)
{
ParticleSystem healed = Instantiate(healthEffect, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
healed.Play();
}
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
UIHealthBar.instance.SetValue(currentHealth / (float)maxHealth);
}
void Launch()
{
GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
Projectile projectile = projectileObject.GetComponent<Projectile>();
projectile.Launch(lookDirection, 300);
animator.SetTrigger("Launch");
PlaySound(shotClip);
}
public void PlaySound(AudioClip clip)
{
audioSource.PlayOneShot(clip);
}
}
Answer by Chrissyeah_ · Sep 25, 2020 at 12:47 AM
Put your movement code in FixedUpdate, this will put it inline with physics update which is more or less fixed. Fixed update does not work well for input commands. But Update is called every frame so the higher your framerate the more frequent it is called. Not good to put your movement code there.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
2D Topdown Character Contoller 0 Answers
character(ball) rolls with less speed when I build it 2 Answers
Why is my character so slow? 1 Answer
Help with my Dash move? 1 Answer