Search Unity

Creating an edit mode where the player can change places of objects to his liking

Discussion in '2D' started by Dumitru, May 27, 2015.

  1. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Hello, so I am making a 2D game where the player has a default set up of his building but he can edit and change the places of the furniture and stairs and rooms, how could I do that? Any help will be appreciated, I'm new to unity and scripting, thank you for taking your time to read the message.
     
  2. Goupi

    Goupi

    Joined:
    May 27, 2015
    Posts:
    9
    I use this script in one of my projects. you need a box collider on your Game Object you want to use.
    (this is C# btw)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(BoxCollider))]
    5.  
    6. public class Drag : MonoBehaviour {
    7.     private Vector3 screenPoint;
    8.     private Vector3 offset;
    9.    
    10.     void OnMouseDown() {
    11.        
    12.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    13.     }
    14.    
    15.     void OnMouseDrag()
    16.     {
    17.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    18.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    19.         transform.position = curPosition;
    20.     }
    21. }
     
  3. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Thank you so much :D but how could I also add the option, that in order to save the set up, the player must complete the level twice without any deaths? That way, it will be sure that the level isn't impossible. Also, how could I change the color to red if the objects can't be put in a specefic place ( in order to not put a bed and a table in the same spot, or near the entrance?
     
  4. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    And after it says, it remplaces the setup the player had before?
     
  5. Goupi

    Goupi

    Joined:
    May 27, 2015
    Posts:
    9
    Heres an idea that I wrote down that could help. Good luck!

    Code (CSharp):
    1. //This isn't supposed to work but just an idea
    2.  
    3. //Also create an empty game object to store this code in
    4.  
    5. public float lives = 2;
    6. public Transform player;
    7.  
    8. void Update(){
    9. //I put -10 because I was assuming you didn't want your player to die
    10. if(player.position.y < -10){
    11. player.position = new Vector3(startX, startY, startZ);
    12. lives = lives-1;
    13. }
    14.  
    15. if(lives == 0){
    16. RestartLevel();
    17. }
    18. }
    19.  
    20. void RestartScene(){
    21. Application.LoadLevel (Application.loadedLevel);
    22. }
     
  6. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80


    Thank you so much!

    Also, off topic but do you have any idea on how to make controls for a space ship like in this demo?

    http://sss135.ru/light2d/RocketExample/RocketExample.html

    I have been spending a few nights on the internet trying to find a guide but I couldn't manage, the point is, I need physics like these but on a jetpack that an Astronaut wears, ofcourse its a 2d game, but I'm stuck at that part, any help will be greatly appreciated, you are so awesome!
     
  7. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Also, i have put the script in c# and it says

    Assets/Scripts/Box Collider Movement.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `Drag'


    What is wrong? :s
     
  8. Goupi

    Goupi

    Joined:
    May 27, 2015
    Posts:
    9
    Rename 'Box Collider Movement.cs' to 'Drag.cs' because in the Drag class code it says
    Code (CSharp):
    1. public class Drag : MonoBehaviour {
    which declares that the class is called Drag and the file needs to be named the same thing. Another good tip for programming is to never have spaces in the names of files or properties because it messes things up. Instead you would name the class BoxColliderMovement.cs.
     
  9. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Alright, thank you so much!