6 Replies - 659 Views - Last Post: 19 April 2017 - 03:39 PM Rate Topic: -----

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Making enemy travel down ward

Posted 17 April 2017 - 02:45 AM

Hey guys I am working on a enemy ai script and there is a certain type of enemy he floats around until he gets to the bottom once he hits left or right wall he changes direction and supposed to eventually end up to the bottom ( this is a clone of the super crate box game) Though I don't know how to make my player go downwards after it turns around. I think its because the first 2 if statements I can't do anything because I used transform.Translate and didn't work I am assuming because the first 2 if statements:

void FixedUpdate()
    {
        if (this.gameObject.name.Equals("ghostEnemy(Clone)"))
        {
            var pos = transform.position;

            if (this.gameObject.GetComponent<SpriteRenderer>().flipX == true)
            {
                Debug.Log("1");
                pos.x -= 25f * Time.deltaTime;
                pos.y = floatPos.y + (Mathf.Sin(Time.time) * floatStrength);
                transform.position = pos;
            }

            if (this.gameObject.GetComponent<SpriteRenderer>().flipX == false)
            {
                Debug.Log("2");
                pos.x += 25f * Time.deltaTime;
                pos.y = floatPos.y + (Mathf.Sin(Time.time) * floatStrength);
                transform.position = pos;
            }

            if (this.gameObject.transform.position.x >= 78.6f)
            {
                Debug.Log("3");
                GetComponent<SpriteRenderer>().flipX = true;
               // pos.x -= 25f * Time.deltaTime;
                //pos.y = floatPos.y + (Mathf.Sin(Time.time) * floatStrength);
                transform.Translate(0, -55 * Time.deltaTime, 0);

            }

            if (this.gameObject.transform.position.x <= -78.6f)
            {
                Debug.Log("4");
                GetComponent<SpriteRenderer>().flipX = false;
            //    pos.x += 25f * Time.deltaTime;
                transform.Translate(0, -55 * Time.deltaTime, 0);
                //pos.y = floatPos.y + (Mathf.Sin(Time.time) * floatStrength);
                //transform.position = pos;
            }


        }


This post has been edited by Dialupp: 17 April 2017 - 02:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Making enemy travel down ward

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Making enemy travel down ward

Posted 17 April 2017 - 06:52 AM

So you want to apply a 'gravity' concept? Why not do it after all your other movement is done?
Was This Post Helpful? 0
  • +
  • -

#3 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Making enemy travel down ward

Posted 17 April 2017 - 03:05 PM

Not sure what you mean? My movement for my other enemy ai is done, but this is part of the movement.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Making enemy travel down ward

Posted 18 April 2017 - 03:42 AM

Some quick unity tips

Don't do this
if (this.gameObject.name.Equals("ghostEnemy(Clone)"))



gameObject.name will allocate memory, and this will add up with you doing it every frame, leading to GC needing to happen. Just store the name as a member variable if you need it although even then I don't really understand the need for it. Surely you are adding this component to objects that you know are the type you need the behaviour for?

You can also go as far as just storing the transform as a member too

Transform thisTransform;
string thisName;

void Awake()
{
     thisName = gameObject.name;
     thisTransform = transform;
}



But I digress. Are these the small floating enemy skulls you are referring to?
Was This Post Helpful? 0
  • +
  • -

#5 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Making enemy travel down ward

Posted 18 April 2017 - 03:51 PM

View Poststayscrisp, on 18 April 2017 - 03:42 AM, said:

Some quick unity tips

Don't do this
if (this.gameObject.name.Equals("ghostEnemy(Clone)"))



gameObject.name will allocate memory, and this will add up with you doing it every frame, leading to GC needing to happen. Just store the name as a member variable if you need it although even then I don't really understand the need for it. Surely you are adding this component to objects that you know are the type you need the behaviour for?

You can also go as far as just storing the transform as a member too

Transform thisTransform;
string thisName;

void Awake()
{
     thisName = gameObject.name;
     thisTransform = transform;
}



But I digress. Are these the small floating enemy skulls you are referring to?



Yeah I already know that but thanks. Yeah they are small floating skulls. I decided to go with new method this code has way too many hardcoded values.

This post has been edited by Dialupp: 18 April 2017 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Making enemy travel down ward

Posted 19 April 2017 - 03:05 PM

If you already know it then you should start thinking about it as otherwise you're just giving yourself more work to do down the line. Just sayin'

From looking at the behaviour of those skulls they look like they essentially are always travelling downwards unless they come close to the floor, if the do come close then they travel up for a short time and then continue floating downwards. Also if they get close to the side of a wall they change direction along with also travelling in the direction of the player.

So really you just want to always be moving downwards and then checking for hitting the floor either by raycasting downwards or checking for collisions, when this happens you could set a moving up state for a time (or for a set amount of upward position change) and then just go back to moving downwards.

bool inUpwardMovementState = false;
		float verticalForce = 5f;
		float maxUpwardAmount = 2f;
		float yAtHit;

		void Update()
		{
			Vector3 pos = transform.position;

			// raycast down and if hit set inUpwardMovementState to true and store the yAtHit

			pos.y += (inUpwardMovementState ? verticalForce : -verticalForce) * Time.deltaTime;

			if (pos.y - yAtHit > maxUpwardAmount)
			{
				inUpwardMovementState = false;
			}

			// blah blah x value follows player

			transform.position = pos;

		}



Something like that.

This post has been edited by stayscrisp: 19 April 2017 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Making enemy travel down ward

Posted 19 April 2017 - 03:39 PM

Thanks I already figured it out yesterday :P I have a way easier method. I used transform.translate() and in the y axis put mathf.Sin + another variable to make it go the direction I want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1