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 Coned_Stoding_games · Feb 23, 2014 at 05:46 AM · c#movementmove

c# script not doing anything.

Hi, I'm trying to write a simple script through experimenting and what not. I wanted to make a script that moves a 2d object around without player input. In this case i called the class enemyMovement, but the script in its simplest form would have been for things like making clouds that move about etc. Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class enemyMovement : MonoBehaviour {
 
     float move = 5;
     public float distance = 0f;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         eneMove ();
     }
     void eneMove()
     {
 
         while (distance < 50) 
         {
 
             transform.Translate (-Vector2.right * move * Time.deltaTime);
             distance++;
         }
 
         if (distance == 50) 
         {
             while(distance > 0)
             {
 
                 transform.Translate (Vector2.right * move * Time.deltaTime);
                 distance--;
             }
         }
     }
 }


I have attactched the code to an object (spheres count as objects yes?) but other than at first when the sphere jumped miles off the screen in one go, it doesn't move at all now. Any ideas on how to make it work how I intended it to would be great.

Thanks for your time.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 23, 2014 at 06:09 AM

You have a couple of issues here. The most pressing is that you are making your while loops happen in a single frame. This causes all the movement to happen at once. One fix for your code would be to only call 'eneMove()' once, and to make the whole method into a Coroutine. Exmaple:

 using UnityEngine;
 using System.Collections;
 
 public class Bug25a : MonoBehaviour {
     
     float move = 5;
     public float distance = 0f;
 
     void Start () 
     {
         StartCoroutine(eneMove());
     }
 
     IEnumerator eneMove()
     {
 
         while (true)
         {
             while (distance < 50) 
             {
                 
                 transform.Translate (-Vector2.right * move * Time.deltaTime);
                 distance++;
                 yield return null;
             }
             
             while(distance > 0)
             {
                 
                 transform.Translate (Vector2.right * move * Time.deltaTime);
                 distance--;
                 yield return null;
             }
         }
     }
 }

But this revised code still has a potential issue. You are measuring your 'distance' by incrementing a variable each frame. In other words, 'distance' is just an frame counter with no map to any real distance. Since the 'deltaTime' of individual frames differ, this code will cause your object to walk around on the 'x' axis some.

Here is an alternate solution for getting an object to move back and forth. It uses Mathf.PingPong():

 using UnityEngine;
 using System.Collections;
 
 public class Swinging : MonoBehaviour {
 
     public Vector3 minPos = new Vector3(-5.0f, 0.0f, 0.0f);
     public Vector3 maxPos = new Vector3( 5.0f, 0.0f, 0.0f);
     public float speed = 1.0f;
 
     void Update()
     {
         transform.position = minPos + (maxPos - minPos) * Mathf.PingPong(Time.time * speed, 1.0f);
     }
 }

Note if you want a more natural feel to the swinging back and forth, you can use Mathf.Sin() in the positioning. Replace the one line of code in Update() with:

 transform.position = minPos + (maxPos - minPos) * (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
Comment
Add comment · Show 1 · 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
avatar image Coned_Stoding_games · Feb 23, 2014 at 07:33 AM 0
Share

Thanks a lot man, really detailed/thorough answer. You've helped me out a lot. :)

avatar image
0

Answer by zharik86 · Feb 23, 2014 at 06:14 AM

Your error is simple. Look attentively at your eneMove function. In it at the same time for one frame two while of a cycle on 50 times everyone (then object will return to start state, but it everything occurs for one frame) will be executed. If you want to move your object, here for example the script is lower:

  public float move = 5.0f;
  public float distance = 0.0f;
  private int directMove = 1; //your direct move, 1 - right, 2 - left

  void Start() {
   distance = 0.0f;
   directMove = 1;
  }

  void Update() {
   eneMove();
  }
  
  public void eneMove() {
   if (directMove == 1) {
    transform.Translate (-Vector2.right * move * Time.deltaTime);
    distance++;
    if(distance > 50) {
     distance = 0.0f;
     directMove = 2;
    }
   } else if (directMove == 2) {
    transform.Translate (Vector2.right * move * Time.deltaTime);
    distance++;
    if(distance > 50) {
     distance = 0.0f;
     directMove = 1;
    }
   }
  }

Somehow so. Certainly instead of conditions of if it is possible to use switch-case. I hope it to you will help.

Comment
Add comment · Show 1 · 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
avatar image Coned_Stoding_games · Feb 23, 2014 at 07:33 AM 0
Share

Thanks for the help :)

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

How can i set a radius for rotatearound ? 0 Answers

Finding the axis of a curving pipe 0 Answers


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