Search Unity

I'm having a few problems with the first game I've made

Discussion in 'Getting Started' started by unity_Tt3gK-5AR5113Q, Feb 17, 2019.

  1. unity_Tt3gK-5AR5113Q

    unity_Tt3gK-5AR5113Q

    Joined:
    Feb 17, 2019
    Posts:
    2
    Hello :)
    I'm new to Unity. I made a very simple example. I want to make myself a game like Can Knockdown with Unity.
    In this example, I've encountered a few problems:

    1 - The ball passes through the cylinders at high speeds and the cylinders are fixed!
    2 - The ball is on the right side of the page but it's still thrown to the left!

    You can see these items in the movies I've attached. Can you guide me?
    Thanks. :):):):):)

    In this case, I move the ball using the keyboard and throw the ball forward (Vector3.forward) with the keyboard keys. But the ball is thrown to the left :



    In this case, I use the mouse to throw the ball, but when the speed of the ball is too high, the ball passes through the cylinders !! :

     
    Last edited: Feb 17, 2019
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    For that last one try increasing the physics timescale. By default the physics engine is set to run 50 passes per second (a value of 0.02) but it's possible the ball is moving fast enough that the physics engine can't keep up (it's running a physics calculation before, but by the time it's ready for another one the ball is already past the cylinders).

    Try doubling the number of passes that are run (by setting it to 0.01) and see if that helps.

    https://docs.unity3d.com/Manual/class-TimeManager.html

    The number you enter is determined by dividing 1,000 (the number of milliseconds in one second) by the number of passes you want to run per second (eg 1,000 divided by 50 is 0.02, 1,000 divided by 100 is 0.01, and so on).

    Additionally the more passes you have the more work your processor has to do. Too many can very easily slow down your game's performance.
     
    Last edited: Feb 17, 2019
  3. unity_Tt3gK-5AR5113Q

    unity_Tt3gK-5AR5113Q

    Joined:
    Feb 17, 2019
    Posts:
    2
    Thank you very much :):)
    Your description was very complete.


    I did the settings that you said, but then my problem was not resolved properly.
    When the "Fixed Timestep" feature is changed from 0.2 to 0.1, the problem is somewhat resolved, but the speed of the ball is reduced. I want to throw the ball at a high speed.

    I uploaded the app source, please check it if you can.
    Have I done the scripting and stage design correctly? This is my first program in Unity.

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Throw_Ball : MonoBehaviour {
    8.  
    9.     public float power = 50f;
    10.     float dragCount; // To control the speed of the ball based on how to drag and drop the ball by the user
    11.     Rigidbody myRigidB;
    12.     Vector3 goRight;
    13.     Vector3 goLeft;
    14.     Vector3 goForward;
    15.     Quaternion myRotaion;
    16.     Vector3 myPosision;
    17.     float oldZ;
    18.     Vector3 screenPoint;
    19.     Vector3 offset;
    20.  
    21.  
    22.  
    23.  
    24.     void OnMouseDown(){
    25.      
    26.         dragCount = 0f;
    27.  
    28.         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    29.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    30.  
    31.     }
    32.  
    33.     void OnMouseDrag(){
    34.  
    35.         Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    36.         Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
    37.  
    38.         if (cursorPosition.y > oldZ) // If it was pulling towards the cylinders
    39.             dragCount++;
    40.         else if (dragCount > 1)
    41.                 dragCount -- ;
    42.  
    43.         oldZ = cursorPosition.y;
    44.  
    45.         if (cursorPosition.x > transform.position.x)
    46.             myRigidB.AddForceAtPosition (goRight / 2 , transform.position);
    47.         else myRigidB.AddForceAtPosition (goLeft / 2, transform.position);
    48.  
    49.     }
    50.     void OnMouseUp(){
    51.      
    52.         myRigidB.Sleep ();
    53.         myRigidB.WakeUp ();
    54.  
    55.         myPosision = transform.position;
    56.         myRotaion = transform.rotation;
    57.  
    58.         Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    59.         Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
    60.  
    61.         if (cursorPosition.y > transform.position.y + 0.1 ){
    62.             dragCount++;
    63.             dragCount = ((cursorPosition.y - transform.position.y) * 50f ) / dragCount;
    64.             if (dragCount > 1.7f)
    65.                 dragCount = 1.7f;
    66.  
    67.  
    68.             if (dragCount > 0.5) {
    69.                 myRigidB.AddForceAtPosition (new Vector3 ((cursorPosition.x - transform.position.x), (cursorPosition.y - transform.position.y)-0.2f , -1) * power * Time.deltaTime * dragCount, transform.position);
    70.                 Invoke ("reloadBall",2f );
    71.             }
    72.  
    73.  
    74.         }
    75.     }
    76.  
    77.     void OnCollisionEnter (Collision col){
    78.         if (col.gameObject.CompareTag ("Enemy")) {
    79.             if (dragCount==1.7f){              
    80.                 Time.timeScale = 0.2f;
    81.                 Invoke ("SetNormalSpeed", 0.18f);
    82.             };          
    83.         }
    84.     }
    85.  
    86.  
    87.  
    88.     // Use this for initialization
    89.     void Start () {
    90.         goForward = transform.TransformDirection (Vector3.forward);
    91.         myRigidB = GetComponent<Rigidbody> ();
    92.         goRight = new Vector3 (1, 0, 0);
    93.         goLeft = Vector3.left;
    94.  
    95.     }
    96.  
    97.  
    98.  
    99.  
    100.     void reloadBall(){      
    101.  
    102.         transform.SetPositionAndRotation (myPosision,myRotaion);
    103.         myRigidB.Sleep ();
    104.         myRigidB.WakeUp ();      
    105.  
    106.     }
    107.     void SetNormalSpeed(){
    108.         Time.timeScale = 1f;
    109.     }
    110.  
    111.  
    112.  
    113.     // Update is called once per frame
    114.     void Update () {
    115.  
    116.         }
    117.  
    118. }
    119.  
    I have used commands myRigidB.Sleep() and myRigidB.WakeUp() to stop ball movement. What should I use to stop ball movement (temporary disable RigidBody component)?

    The next problem with this game is that the ball does not throw the right position. Is there any problem with my coding? When the ball is in the middle of the page, this problem gets less, but when the ball is to the right of the page and the target cylinders do not hit the target area.

    Thank you again :)
     

    Attached Files:

    Last edited: Feb 20, 2019