- Home /
build and test runs differnent,test run and build diffrent
so I have this game that you play as a cube on top of a platform while other cubes charge towards you and try to knock you of the platform. I have the enemy in a prefab that I summon with a simple spawner script every time and the movement for the enemy is here
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class enemyScript : MonoBehaviour{
public Rigidbody rb;
public float speed = 1f;
GameObject player;
void Start(){
player = GameObject.Find("player");
}
// Update is called once per frame
void Update()
{
Vector3 way = (player.transform.position - transform.position) * Time.deltaTime;
way.y = 0;
rb.AddForce(Vector3.Normalize(way)*speed*20);
}
}
the problem is that when I run it as a simple test the enemies move in enormously fast and then when I build the same game the enemies move enormously slow. is this a well known problem or am I doing something wrong? ,so I have this game that you play as a cube on top of a platform while other cubes charge towards you and try to knock you of the platform. i have the enemy in a prefab that I summon with a simple spawner script every time and the movement for the enemy is here
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class enemyScript : MonoBehaviour{
public Rigidbody rb;
public float speed = 1f;
GameObject player;
void Start(){
player = GameObject.Find("player");
}
// Update is called once per frame
void Update()
{
Vector3 way = (player.transform.position - transform.position) * Time.deltaTime;
way.y = 0;
rb.AddForce(Vector3.Normalize(way)*speed*20);
}
}
the problem is that when I run it as a simple test the enemies move in enormously fast and then when I build the same game the enemies moves enormously slow. is this a well known problem or am I doing something wrong.
Answer by DenisIsDenis · May 06, 2021 at 11:01 AM
Hello. Add force function accelerates the movement of an object, and the more times this function is called, the more the speed of the object. I recommend using the following code (in FixedUpdate):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyScript : MonoBehaviour
{
public Rigidbody rb;
public float speed = 1f;
GameObject player;
void Start()
{
player = GameObject.Find("player");
}
void FixedUpdate()
{
Vector3 way = player.transform.position - transform.position;
way = new Vector3(way.x, 0, way.z);
way = way.normalized;
rb.velocity = way * speed * 2;
// the speed of the game object will be constant and depend on the variable "speed"
}
}
Your answer
Follow this Question
Related Questions
CustomRenderTexture (RFloat) wont initialize in the same frame it is created. 0 Answers
Exiting an if statement when calling a function from another script (Possible bug) 1 Answer
Unity not compiling script changes 2 Answers
Mathf.Sin only returning positive values? 2 Answers
Camera.main.ScreenToWorldPoint not working as expected 1 Answer