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 /
avatar image
0
Question by DubstepDragon · Aug 18, 2015 at 02:36 PM · movementobjectenemyspawngrid

Enemy object spawning before move action is complete :/

My character can move forwards, backwards, left, right, and rotate 90 degrees left and right. It's a grid-based game, and I have everything in order for my prototype, except one issue: The enemies, what have a chance to spawn on movement, end up spawning before the move action is complete, so it spawns then the character moves forwards and ends up inside the enemy object... Additionally, if it spawns from moving backwards or sideways, I want the player to be rotated to face that way before the movement restriction is activated... It will be more clear when you see my code below. This is the script that handles most of these things I've specified. I'd greatly appreciated it if you can help me have the enemy spawn after the player has finished moving, and to rotate to face the enemy if it spawned from moving at a direction other than forwards. Thanks <3

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public static bool CanMoveForward;
     public static bool CanMoveBackward;
     public static bool CanMoveLeft;
     public static bool CanMoveRight;
     public static bool CanRotateLeft;
     public static bool CanRotateRight;
 
     public static bool CanMoveForwardCache;
     public static bool CanMoveBackwardCache;
     public static bool CanMoveLeftCache;
     public static bool CanMoveRightCache;
 
     public static int chancetospawn;
 
     public static bool CanSpawnEnemy = true;
 
     public GameObject Enemy1;
     public GameObject Enemy2;
     public GameObject Enemy3;
     public GameObject Enemy4;
     public GameObject Enemy5;
     public GameObject Enemy6;
     public GameObject Enemy7;
     public GameObject Enemy8;
     public GameObject Enemy9;
     public GameObject Enemy10;
 
     public static GameObject EnemyObject;
 
     public GameObject EnemySpawner;
 
     //Translation:
     float movSpeed = 4.0f;
     Vector3 pos;
     Transform tr ;
     public static bool moving = false;
     
     //Rotation:
     public static bool rotating = false;
     public float rotSpeed = 360f;
     float rotDegrees = 0f;
     Quaternion rotToAngle ;
     
     void Start () {  
         pos = transform.position;
         tr = transform;
 
         CanMoveForward = true;
         CanMoveBackward = true;
         CanMoveLeft = true;
         CanMoveRight = true;
         CanRotateLeft = true;
         CanRotateRight = true;
     }
 
     void SpawnEnemy() {
         CanMoveForwardCache = CanMoveForward;
         CanMoveBackwardCache = CanMoveBackward;
         CanMoveLeftCache = CanMoveLeft;
         CanMoveRightCache = CanMoveRight;
 
         CanMoveForward = false;
         CanMoveBackward = false;
         CanMoveLeft = false;
         CanMoveRight = false;
         CanRotateLeft = false;
         CanRotateRight = false;
 
         int i = Random.Range (1, 10);
 
         if(i == 1) {
             EnemyObject = Enemy1;
             EnemyStats.enemyhealth = 20;
             EnemyStats.enemydamage = 3;
         }
         if(i == 2) {
             EnemyObject = Enemy2;
             EnemyStats.enemyhealth = 25;
             EnemyStats.enemydamage = 5;
         }
         if(i == 3) {
             EnemyObject = Enemy3;
             EnemyStats.enemyhealth = 30;
             EnemyStats.enemydamage = 5;
         }
         if(i == 4) {
             EnemyObject = Enemy4;
             EnemyStats.enemyhealth = 35;
             EnemyStats.enemydamage = 6;
         }
         if(i == 5) {
             EnemyObject = Enemy5;
             EnemyStats.enemyhealth = 40;
             EnemyStats.enemydamage = 7;
         }
         if(i == 6) {
             EnemyObject = Enemy6;
             EnemyStats.enemyhealth = 50;
             EnemyStats.enemydamage = 8;
         }
         if(i == 7) {
             EnemyObject = Enemy7;
             EnemyStats.enemyhealth = 100;
             EnemyStats.enemydamage = 10;
         }
         if(i == 8) {
             EnemyObject = Enemy8;
             EnemyStats.enemyhealth = 100;
             EnemyStats.enemydamage = 20;
         }
         if(i == 9) {
             EnemyObject = Enemy9;
             EnemyStats.enemyhealth = 100;
             EnemyStats.enemydamage = 20;
         }
         if(i == 10) {
             EnemyObject = Enemy10;
             EnemyStats.enemyhealth = 100;
             EnemyStats.enemydamage = 20;
         }
 
         Instantiate(EnemyObject, EnemySpawner.transform.position, Quaternion.identity);
 
     }
 
 
 
     // Update is called once per frame
     void Update () {
 
 //        Debug.Log (CanSpawnEnemy);
 
         Debug.DrawRay(transform.position, transform.forward, Color.red);
         //Input:
         if (!moving && !rotating) {
             if (Input.GetKey(KeyCode.D) && tr.position == pos && CanMoveRight) {
                 pos += transform.right;
                 moving=true;
                 chancetospawn = Random.Range(1, 10);
                 if(chancetospawn == 1 && CanSpawnEnemy == true) {
                     SpawnEnemy();
                 }
             } else if (Input.GetKey(KeyCode.A) && tr.position == pos && CanMoveLeft) {
                 pos += -transform.right;
                 moving=true;
                 chancetospawn = Random.Range(1, 10);
                 if(chancetospawn == 1 && CanSpawnEnemy == true) {
                     SpawnEnemy();
                 }
             } else if (Input.GetKey(KeyCode.W) && tr.position == pos && CanMoveForward) {
                 pos += transform.forward;
                 moving=true;
                 chancetospawn = Random.Range(1, 10);
                 if(chancetospawn == 1 && CanSpawnEnemy == true) {
                     SpawnEnemy();
                 }
             } else if (Input.GetKey(KeyCode.S) && tr.position == pos && CanMoveBackward) {
                 pos += -transform.forward;
                 moving=true;
                 chancetospawn = Random.Range(1, 10);
                 if(chancetospawn == 1 && CanSpawnEnemy == true) {
                     SpawnEnemy();
                 }
             } else if (Input.GetKey(KeyCode.Q) && tr.position == pos && CanRotateLeft) {
                 rotDegrees -= 90f;
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
             } else if (Input.GetKey(KeyCode.E) && tr.position == pos && CanRotateRight) {
                 rotDegrees += 90f;
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
             }
         }
 
 
         
         //Translation:
         if (moving) {
             if (Vector3.Distance(transform.position,pos) <0.05f){
                 transform.position = pos;
                 moving=false;
 //                print ("FINISHED MOVE");
             } else {
                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
             }
         }
         
         //Rotation:
         if (rotating) {
             if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
                 transform.rotation = rotToAngle;
                 rotating=false;
 //                print ("FINISHED TURN");
             } else {
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
             }
         }
     }
 
 }
 
Comment
Add comment · Show 5
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 Runalotski · Aug 18, 2015 at 02:57 PM 0
Share

Delete all your spawn Enemy Calls and place One on line 98 where it says finished turn OR on 87 where it says finisehd move.

avatar image DubstepDragon · Aug 18, 2015 at 03:05 PM 0
Share

@Runalotski Can you please show me? I'm a little confused :/

Thanks :)

avatar image DubstepDragon · Aug 18, 2015 at 07:28 PM 0
Share

Bump!

:3

avatar image Runalotski · Aug 18, 2015 at 08:13 PM 1
Share

you use the method SpawnEnemy() at the same time you move the character.

so delete all of the SpawnEnemy() in your code

then write one in at the end so you will have

      //Translation:
              if (moving) {
                  if (Vector3.Distance(transform.position,pos) <0.05f){
                      transform.position = pos;
                      moving=false;
      //                print ("FINISHED $$anonymous$$OVE");
                  } else {
                      transform.position = Vector3.$$anonymous$$oveTowards(transform.position, pos, Time.deltaTime * movSpeed);
                  }
              }
              
              //Rotation:
              if (rotating) {
                  if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
                      transform.rotation = rotToAngle;
                      rotating=false;
      //                print ("FINISHED TURN");
 /////////////// this is what has changed
                        SpawnEnemy();
                  } else {
                      transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
                  }
              }
 
avatar image DubstepDragon · Aug 20, 2015 at 03:12 PM 0
Share

I'll try it tonight when I'm back home :)

Also please convert your comment into an answer so that I can mark it as answered if it works :D

Thanks a lot :D

1 Reply

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

Answer by Runalotski · Aug 20, 2015 at 03:34 PM

you use the method SpawnEnemy() at the same time you move the character.

so delete all of the SpawnEnemy() in your code

then write one in at the end so you will have

   //Translation:
               if (moving) {
                   if (Vector3.Distance(transform.position,pos) <0.05f){
                       transform.position = pos;
                       moving=false;
       //                print ("FINISHED MOVE");
                   } else {
                       transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
                   }
               }
               
               //Rotation:
               if (rotating) {
                   if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
                       transform.rotation = rotToAngle;
                       rotating=false;
       //                print ("FINISHED TURN");
  /////////////// this is what has changed
                         SpawnEnemy();
                   } else {
                       transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
                   }
               }
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 DubstepDragon · Aug 26, 2015 at 08:02 PM 0
Share

Works perfectly, thanks! :D

:3

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

26 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

Related Questions

Roll-A-Ball enemyball[Personal Extra] Cant get it to follow the player 2 Answers

Enemy Spawning and Movement 0 Answers

Spawning different random objects at the same position? 2 Answers

More efficient way to spawn enemies in my game...? 2 Answers

Issue with manual collision and mobs spawning... 1 Answer


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