Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Need Help With Game

Discussion in 'Getting Started' started by HuskyStudiosLTD, Nov 9, 2019.

  1. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    Hi, I’m working on a game where you tap the screen and a cylinder drops but I’m struggling to get my code to work. Has anyone got any ideas for code that I can use?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    Your question is too general. What's holding the cylinder up? Can't you just disable it (with gameObject.SetActive(false)) when the screen is tapped? Or do you mean to spawn a new cylinder (in which case the code you need is Instantiate)?

    Show what you've done, and what trouble you're having with it, and you can get a more specific answer.
     
  3. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    Spawn a new cylinder
     
  4. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class TouchScript : MonoBehaviour {
    7.  
    8.    public GameObject Cylinder;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        Touch myTouch = Input.GetTouch (0);
    20.         for (int i = 0; i < Input.touchCount; i++)
    21.             if (myTouch.phase == TouchPhase.Began)
    22.             {
    23.                 {
    24.                     Debug.Log("Touch Position" + myTouch.Postition);
    25.                     SpawnCylinder(myTouch);
    26.                 }
    27.             }
    28.     }
    29.     void SpawnCylinder(Touch dropTouch)
    30.     {
    31.     Vector3 touchPos = Camera.main.ScreenToWordPoint (dropTouch.Position);
    32.     touchPos.z = 1;
    33.     Debug.Log ("Vector3 Pos" + touchPos);
    34.     Instantiate (Cylinder, touchPos, Quaternion.identity);
    35.     }
    36. }
     
  5. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    Okay so after some digging around on the internet I found a script that spawns the Cylinder when I tap the screen. But its spawning them all around the place is it possible for me to make them spawn in a certain area. Here's the code. And sorry I'm a total noob.
    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     public GameObject Cylinder;
    4.  
    5.     // Use this for initialization
    6.     void Start()
    7.     {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.  
    15.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    16.         {
    17.             RaycastHit hit;
    18.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    19.  
    20.             if (Physics.Raycast(ray, out hit))
    21.                 if (hit.collider != null)
    22.  
    23.                    Instantiate(Cylinder, hit.point, transform.rotation);
    24.         }
    25.  
    26.     }
    27. }
    28.  
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    It's not spawning all over the place, it's spawning where you tap. Line 18 gets a ray from the touch position; line 20 collides this with something (you must have some collider in the scene, I guess), and line 23 instantiates your cylinder at the point that was hit.

    If you don't want it to do that, then don't bother with all that. Try this Update method instead:

    Code (csharp):
    1. void Update() {
    2.     if (Input.GetMouseButtonDown(0)) {
    3.         Instantiate(Cylinder);
    4.     }
    5. }
    Untested, but I'm pretty sure if you don't specify a position and orientation, it will spawn the object in whatever position and orientation the prefab has, which is probably what you want.
     
  7. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    It's very close to what i want but what I'm trying to get is for it to drop at a consistant height and for it to drop along the Z axis if that makes any sense.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    It should be dropping from whatever the height (position Y value) of your prefab is. So just adjust that until it's where you want it.

    As for dropping along the Z axis... that suggests you have a very unusual setup. Things fall in the direction of gravity, which really needs to be in the -Y direction, if you're going to use physics at all. Do you want to explain more about what you're trying to do, and maybe we can help you avoid some pitfalls?
     
  9. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    Would it be possible to make a certain area of the world touch only so the cylinder can spawn in a small square?
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    Yes, but now you'll need to go back to something like the original code at the top of this thread. That is, you need to get a ray through the screen at the touch (or mouse) position, and see where this intersects a target collider. For starters, just put a Plane up where you want the touches to work; then later you can just turn off the MeshRenderer component, so the plane is invisible, but it still restricts where you can touch.
     
  11. HuskyStudiosLTD

    HuskyStudiosLTD

    Joined:
    Jan 21, 2019
    Posts:
    7
    So how would I code that? Would I change the camera to the Plane? And also the cylinders are not falling from the sky they're spawning from the cylinder that's been placed
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    Code it exactly like the code in the top of this thread. The only collider in the scene should be the plane. If you need other colliders, and find that you can also touch those and you don't want that to be possible, then post back and we'll talk about using layers to limit what the Physics.RayCast call casts against.