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);
}
}
} ,
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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