Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 · Feb 22, 2015 at 05:37 PM · movementspawngridstop

Issue with manual collision and mobs spawning...

My character has four colliders on every side that detect if the player is colliding with a wall to stop movement in that given direction. My game also has a random enemy encounter mechanic, where every step moved has a 1 out of 10 chance to trigger a sequence where all movement is frozen, an enemy spawns, and after the enemy is destroyed, movement is resumed again. I have several scripts that control this. My problem is, when a mob spawns, movement is not stopped instantly; rather, the player moves one block forward. Is there a possible way to make the random roll at the end of the movement cycle so the player's movement is halted INSTANTLY after the mob has spawned?

Thanks in advance!

Here are all the scripts associated with the issue:

FRONT COLLISION SYSTEM (other three [back, left, right] are very similar):

 using UnityEngine;
 using System.Collections;
 
 public class FrontCollisionSystem : MonoBehaviour {
 
     public bool CanPickupItem = false;
 
    void OnTriggerStay(Collider other) {
         if(other.CompareTag("wall")) {
             PlayerMovement.CanMoveForward = false;
         }
 
         if(other.CompareTag("item")) {
             CanPickupItem = true;
         }
     }
 
     void OnTriggerExit(Collider other) {
         if(other.CompareTag("wall")) {
             PlayerMovement.CanMoveForward = true;
             CanPickupItem = false;
         }
     }
 
 }
 


PLAYER MOVEMENT:

 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;
         }
         if(i == 2) {
             EnemyObject = Enemy2;
             EnemyStats.enemyhealth = 25;
         }
         if(i == 3) {
             EnemyObject = Enemy3;
             EnemyStats.enemyhealth = 30;
         }
         if(i == 4) {
             EnemyObject = Enemy4;
             EnemyStats.enemyhealth = 35;
         }
         if(i == 5) {
             EnemyObject = Enemy5;
             EnemyStats.enemyhealth = 40;
         }
         if(i == 6) {
             EnemyObject = Enemy6;
             EnemyStats.enemyhealth = 50;
         }
         if(i == 7) {
             EnemyObject = Enemy7;
             EnemyStats.enemyhealth = 100;
         }
         if(i == 8) {
             EnemyObject = Enemy8;
             EnemyStats.enemyhealth = 100;
         }
         if(i == 9) {
             EnemyObject = Enemy9;
             EnemyStats.enemyhealth = 100;
         }
         if(i == 10) {
             EnemyObject = Enemy10;
             EnemyStats.enemyhealth = 100;
         }
 
         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);
             }
         }
     }
 
 }
 



ENEMY STATS:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyStats : MonoBehaviour {
 
     public static int enemyhealth = 100;
 
     // Use this for initialization
     void Start () {
 //        PlayerStats.inCombat = true;
     }
     
     // Update is called once per frame
     void Update () {
         if(enemyhealth <= 1) {
             PlayerMovement.chancetospawn = 0;
 
             PlayerMovement.CanMoveForward = PlayerMovement.CanMoveForwardCache;
             PlayerMovement.CanMoveBackward = PlayerMovement.CanMoveBackwardCache;
             PlayerMovement.CanMoveLeft = PlayerMovement.CanMoveLeftCache;
             PlayerMovement.CanMoveRight = PlayerMovement.CanMoveRightCache;
             PlayerMovement.CanRotateLeft = true;
             PlayerMovement.CanRotateRight = true;
 
             PlayerStats.CurrentXP += 50;
 
             if (gameObject != null) {
                 Destroy(this.gameObject);
             }
         }
     }
 }
 


Thanks in advance! :D

Comment
Add comment · Show 1
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 · Feb 23, 2015 at 07:28 PM 0
Share

pingpingpingpingping!

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by jpthek9 · Feb 24, 2015 at 12:47 AM

Sorry I can't prune through all that code but the general idea is to have a flag that controls whether or not the player can move.

 bool CanMove = true;
 
 void OnTriggerEnter()
 {
 if (Random.Range(0,10) == 0)
     {
         CanMove = false;
     }
 }
 
 void Update()
 {
     if (CanMove)
     {
         DoMoveStuff();
     }
 }

When the enemy dies and you want the player to be able to move again, just set CanMove = true.

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

21 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

Related Questions

Enemy object spawning before move action is complete :/ 1 Answer

Object going through another....How to solve it? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Spawn object problem 1 Answer

How to apply more than one trigger on a single gamebject 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