Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by The_last_trico · May 04, 2021 at 06:36 PM · scripting problembug-perhaps

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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"
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges