Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Ruki01 · Apr 25, 2018 at 12:31 AM · movinggameobject

Trying to get object to move back forth,

Hi, I'm trying to get my enemy object to move back and forth but it doesn't seem to be working with this code. Anyone else know what the problem with this code is or can maybe suggest an alternative? I'm VERY new to unity at this point.

public class EnemyC : MonoBehaviour {

 public float moveSpeed = 10f;
 private float  CurrentPos;
 void Start ()
 {
     

 }
 
 
 void Update () {

     CurrentPos = transform.position.x;
     while (CurrentPos <= 2f)
     {
         transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
     }
     if (CurrentPos>= 2f)
     {
         transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
     }
 }
 

} ,

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

Answer by tormentoarmagedoom · Apr 25, 2018 at 08:07 AM

Good day.

First, as a recomendations:

If you are new at unity, declare a public variable like moveSpeed, assign its value in the Start(), not when delcaring (will prevent problems if change the value via inspector. You can do it when have more control of Unity envoirment).

  public float moveSpeed;
  private float  CurrentPos;
  void Start ()
  {
      moveSpeed = 10;
  }


A float value without decimals, don't need the "f".


And your problem is: You are using a while loop, this means the code will be looping the while during the same update, the code will not continue until while condition is false. I think you want to replace that "while" for a "if", and the "if" for an "else if". And you cant say the "=" in both of them, i mean, when Currentpos == 2, what will do ? So, i supose you need this:

  void Update () {
      CurrentPos = transform.position.x;
      if(CurrentPos < 2f)
      {
          transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
      }
      else if(CurrentPos> 2f)
      {
          transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
      }
  }


IF helped, accept the answer and close the question!

BYE! :D

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
avatar image
0

Answer by vinilly · Apr 25, 2018 at 09:01 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyC : MonoBehaviour {
 
     //Logic
 
 
     //Floats
     public float moveSpeed = 10f;
     //private float CurrentPos;
     public float _distance = 10f;
 
     //Bools
     private bool _isGoingLeft = false;
 
     //Vectors
     private Vector3 _velocity;
     private Vector3 _originalPosition;
 
     //Components
     private Transform _transform;
 
     void Start()
     {
         //Get Original Position on Start
         _originalPosition = transform.position;
         //Get local transform Component
         _transform = GetComponent<Transform>();
         //Velocity is movement
         _velocity = new Vector3(moveSpeed, 0f, 0f);
     }
 
 
     void Update()
     {
         float _distFromStart = transform.position.x - _originalPosition.x;
 
 
         //CurrentPos = transform.position.x;
         /*
         while (CurrentPos <= 2f)
         {
             transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
         }
 
 
         if (CurrentPos >= 2f)
         {
             transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
         }
         */
         
         if (_isGoingLeft)
         {
             // If gone too far, switch direction
             if (_distFromStart > _distance)
                 SwitchDirection();
 
             _transform.Translate(_velocity.x * moveSpeed * Time.deltaTime, 0f, 0f);
             transform.localScale = new Vector3(1f, 1f, 1f);
         }
 
         else
         {
             // If gone too far, switch direction
             if (_distFromStart < -_distance)
                 SwitchDirection();
 
             _transform.Translate(-_velocity.x * moveSpeed * Time.deltaTime, 0f, 0f);
             transform.localScale = new Vector3(-1f, 1f, 1f);
         }
     }
 
     //Switch directions after meeting the distance
     void SwitchDirection()
     {
         _isGoingLeft = !_isGoingLeft;
         //TODO: animation, etc
     }
 }


Took me half an hour but here you go.

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

136 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

Related Questions

Game object position is stuck when using mathf.sin,Object's position stuck when using Math.sin 0 Answers

Creating a train system with little to no code knowledge 0 Answers

How to move a ball to left and when it hits the wall should go towards right and thus continues? 0 Answers

I need help with my flying script 0 Answers

moving object dissapears when it reaches it's destination 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