Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when I command or set an enemy to go left and then at reaching a particular state or position I say my enemy to go right but it is not going right and not reaching to perfect position here is the code I think there is no issue in the code but it is not working properly so here are some images of the code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class enem: MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
        }

        // Update is called once per frame
        void Update()
        {
            transform.position = transform.position + new Vector3(-3f, 0f, 0f)* 1 * Time.deltaTime;
            if (transform.position.x < -29)
            {
                transform.position = transform.position + new Vector3(3f, 0f, 0f) * 1 * Time.deltaTime;
    

            }
        }
    }


What I have tried:

i have tried a lots of the ways to solve this its about a game engine called unity game engine
Posted
Updated 8-Sep-21 6:48am
v2
Comments
Richard MacCutchan 8-Sep-21 9:56am    
"I think there is no issue in the code but it is not working properly"
Can both of those be true? Are you sure that if (transform.position.x > -29) is correct? Have you used debug tools to check the values of the position at each step?

1 solution

Your code is decrementing x. Consider what happens in the two possible cases:

Option 1:
x is greater than -26:
    • x == -25
    • x = x - 3 == -28
    • -28 > -29, so the position is increased by 3: x = x + 3 == -25
    • x == -25
    • x = x - 3 == -28
    • -28 > -29, so the position is increased by 3: x = x + 3 == -25
    • x == -25
    • x = x - 3 == -28
    • -28 > -29, so the position is increased by 3: x = x + 3 == -25
  1. ...


Option 2:
x is less than or equal to -26:
    • x == -26
    • x = x - 3 == -29
    • -29 ≤ -29, so the position is kept;
    • x == -29
    • x = x - 3 == -32
    • -32 ≤ -29, so the position is kept;
    • x == -32
    • x = x - 3 = -35
    • -35 ≤ -29, so the position is kept;
  1. ...



You need to work out what you are actually trying to do, and then fix your logic to match.
 
Share this answer
 
v4
Comments
Lemons and Giggles 8-Sep-21 12:24pm    
thanks bro for this solution but even if I try pos == -29 than may be player reaches at the -28.99 not -29
Richard Deeming 8-Sep-21 12:31pm    
Read my answer again, and think through what you're asking the code to do.

As it stands, either the player moves and immediately moves back, or they keep moving left.

HINT: Read up on the difference between < (less than) and > (greater than). Then step through your code line by line to see what's happening.
Lemons and Giggles 8-Sep-21 12:42pm    
ok bro thanks but i know the difference between less than and greater than bro but player actually is sticking there and not moving either left or right and sometimes it is trying to go left and right as I change the value
Richard Deeming 9-Sep-21 3:17am    
Well you clearly don't. Your code says:

* Move the player left;
* If the player is to the right of a given position, move them back to the right;
* If the player is to the left of that position, keep moving left;

As I said, you need to think carefully about what you are trying to do, and step through your code line by line to see what it's actually doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900