Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My Fist Game! Please Help Me!

Discussion in 'Getting Started' started by Samantha_K, Mar 4, 2017.

  1. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi

    I startet to learn unity for 1 year ago and still not managed to put together a full mobile game. I start my development many times and get stuck, i get depressed and i quit. So now i am going to try for the last time. I am going to post all my game development process here in this post. Please give feedback to my development process

    What i am going to make this time is for only learning process. So iam going to copy game like The line Zen



    What i have done so far
    -Player Script
    -MoveingPlatform

    My first question;
    Should i make the ball move forward or make platform(level) move as i have done it now?

    If ball is moving forward:
    How can i spawn random levels easily(Object Pooling)?
    How will this affect gameplay?.
    Can use DontGoThroughThings.cs if ball is moving forward.


    If Platform(level) moving backward

    How can i spawn random levels easily(Object Pooling)?
    How will this affect gameplay?.
    I cant use DontGoThroughThings.cs if ball is not moving forward.

    Player Script: Ball can only be moved left and right.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     [SerializeField] private float ballSpeed = 800;
    7.  
    8.     private Rigidbody myRB;
    9.  
    10.     void Awake(){
    11.         myRB = GetComponent<Rigidbody> ();
    12.     }
    13.  
    14.     void FixedUpdate(){
    15.         float moveH = Input.GetAxis ("Horizontal");
    16.         Vector3 movement = new Vector3 (moveH, 0f, 0f);
    17.         myRB.velocity = (movement*ballSpeed*Time.deltaTime);
    18.     }
    19. }
    Anything i should do differently in Player script?

    Moving Platform: This is attached to every level.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovingObject : MonoBehaviour {
    5.  
    6.     public float speed = 200;
    7.  
    8.     void Update () {
    9.         Move ();
    10.     }
    11.  
    12.     void Move(){
    13.         Vector3 movement =  Vector3.back * (speed / 10) * Time.deltaTime;
    14.         transform.Translate (movement, Space.World);
    15.     }
    16. }
     

    Attached Files:

  2. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    @Samantha_K I'm very new to Unity and this may or may not be correct, but I noticed in the Player script and the Moving Platform script your floats do not have the letter f after the value you give it.

    Another thing you might try is the Rolling Ball tutorial on Unity. That may solve the problem of the ball not moving forward.

    Good luck with your project. I hope either of those help you solve part of your problem.
     
    Samantha_K likes this.
  3. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Thank you for your reply. Iam very very slow learner. :(
     
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    It's perfectly acceptable to leave off the float designator on whole numbers.

    public float speed = 200; - Does not cause an error

    public float speed = 200.0; - Does cause an error

    @Samantha_K there are tutorials on instantiating and object pooling on the interwebs. This subject is too big and too complicated to provide a quick answer and this something that you must learn on your own. As for your scripts, do you have any specific problems? There are more ways to do something and usually no right way. If your scripts are working properly, leave them alone and focus on you next real problem.
     
    DocJ likes this.
  5. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi @Bill_Martini Thank you for your reply. I understand that Object pooling is complicated and i have to learn that on my own. What iam actually asking is what is right method to use? Object pooling or any other way?

    A level is empty gameObject (ex: Level01) with child gameobjects. Child Gameobjects are pre designed pattern. Each levels has diffrent patterns. Patterns are made with moving and rotating objects, some objects (Green boxes) has Rigidbody attached.

    What iam going to try now is create ObjectPooling scripts and attache that to parent GameObject (Level01,leve02...). Thank you for your time @Bill_Murrey and @DocJ
     
    DocJ likes this.
  6. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    See? I told you I was new. Lol. Now I learned something new too.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @Samantha_K, you are putting the cart way ahead of the horse. Object pooling is something you worry about when your game is pretty much done and you have profiled it and found that object creation is a bottleneck.

    You haven't done any of that yet, so you should not be worrying about object pooling.

    Instead, you should be focused on making things appear on the screen and move around the way you want them to. And you should do this in the simplest way possible. At every turn, ask yourself "how many ways can I think of to do this?" Then...
    • If the answer is zero, do some more tutorials, or ask for help.
    • If the answer is one, then ask yourself "but could there be an easier way?" If not sure, do some more tutorials, or ask for help.
    • If the answer is "lots," then look at each one and see which is the simplest way. Do it that way.
    Usually there is a quite simple way to do things in Unity, so if something looks really complicated, it's possible you are missing a simpler approach.

    Remember, the greatest risk to your project is that you will never finish it. So don't be ashamed to cut corners, simplify your design, and always do the bare minimum! Your goal is to get to the end of the project. And I know you can do it!
     
    Ryiah, OboShape, Samantha_K and 4 others like this.
  8. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    @JoeStrout THANK YOU SO MUCH!. Now i understand why i am failing all the time. I think too much and make it too complicated for me and i get depressed. This apply not only for my gamedev, I think too much about anything. I greatly appreciate your kind words. @JoeStrout. Thank you again. Iam going to try to do simple tings first.
     
    JoeStrout likes this.
  9. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    I try to think of it like writing a novel. The key is to get the 50,000+ words on paper. They don't have to be the best words, or even really good words, but you have to have that story written out before you can go back and polish it. You can't polish what doesn't exist in other words. So when writing you must turn off your internal editor and just write the words.

    Finishing your game is similar. Don't worry too much about art or music or polish. Just get things on the screen, get them moving, make the game flow the way you want it to. Once you have it done, then you can go back and spend all of the time you want to making it pretty.
     
    Samantha_K and JoeStrout like this.
  10. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi, Thank you @Dave-Carlile.

    Now i started to work on my movingObstacles. To answer my own question
    I found out that i should make ball(player) move forward so that ball can hit object with rigidbody attached to.

    I have been working on movingObstacles today and here is what i have done so far:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovingObstacle : MonoBehaviour {
    6.  
    7.     [Header("Rotate Object")]
    8.     [SerializeField] private Vector3 rotateXYZ;
    9.     [SerializeField] private bool randomRotateSpeed;
    10.     [SerializeField] private Vector2 randomRotateSpeedMinMax;
    11.     [Range(0, 5)]
    12.     [SerializeField] private float rotatingSpeed;
    13.  
    14.     [Header("Move Object")]
    15.     [SerializeField] private bool canMove;
    16.     [SerializeField] private Vector3 moveLength;
    17.     [SerializeField] private bool randomMoveSpeed;
    18.     [SerializeField] private Vector2 randomMoveSpeedMinMax;
    19.     [Range(0, 3)]
    20.     [SerializeField] private float speed = 1;
    21.     private Vector3 startPosMovingObj;
    22.     private Vector3 movementLength;
    23.  
    24.     [Header("PopUp Object")]
    25.     [SerializeField] private bool canPopUp;
    26.     [SerializeField] private bool randomPopUpSpeed;
    27.     [SerializeField] private Vector2 randomPopUpSpeedMinMax;
    28.     [Range(0, 4)]
    29.     [SerializeField] private float popUpSpeed;
    30.     [SerializeField] private Vector3 popUpLength;
    31.     [SerializeField] private float wakeUpInn;
    32.     private Vector3 startPosPopUpObj;
    33.  
    34.     void Start(){
    35.         StartPosition ();
    36.         RandomSpeed ();
    37.  
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update () {
    42.         Rotate ();
    43.         PingPong ();
    44.         PopUp ();
    45.     }
    46.  
    47.     void Rotate(){
    48.         transform.Rotate(rotateXYZ*rotatingSpeed*Time.deltaTime);
    49.     }
    50.  
    51.     void PingPong(){
    52.         if (canMove == true) {
    53.             float pingPong = Mathf.PingPong(Time.time * speed, 1);
    54.             transform.position = Vector3.Lerp(startPosMovingObj, movementLength, pingPong);
    55.         }
    56.     }
    57.  
    58.     void PopUp(){
    59.         if (canPopUp==true) {
    60.             float step = popUpSpeed * Time.deltaTime;
    61.             transform.position = Vector3.MoveTowards (transform.position, startPosPopUpObj, step);
    62.         }
    63.     }
    64.  
    65.     void StartPosition(){
    66.         //Get start position of Pop up object.
    67.         startPosPopUpObj = new Vector3 (transform.position.x + popUpLength.x, transform.position.y + popUpLength.y, transform.position.z + popUpLength.z);
    68.         //Get start position of moving object.
    69.         startPosMovingObj = this.transform.position;
    70.         Vector3 temp = moveLength;
    71.         movementLength =  new Vector3 (transform.position.x + temp.x, transform.position.y + temp.y, transform.position.z + temp.z);
    72.     }
    73.  
    74.     void RandomSpeed(){
    75.         if (randomPopUpSpeed == true) {
    76.             float randomSpeed = Random.Range (randomPopUpSpeedMinMax.x, randomPopUpSpeedMinMax.y);
    77.             popUpSpeed = randomSpeed;
    78.         }
    79.         if (randomMoveSpeed == true) {
    80.             float randomSpeed = Random.Range (randomMoveSpeedMinMax.x, randomMoveSpeedMinMax.y);
    81.             speed = randomSpeed;
    82.         }
    83.         if (randomRotateSpeed == true) {
    84.             float randomSpeed = Random.Range (randomRotateSpeedMinMax.x, randomRotateSpeedMinMax.y);
    85.             rotatingSpeed = randomSpeed;
    86.         }
    87.     }
    88. }
    i know that code is not very clean.. :(. but iam still working on it. This script is working nice so far :) .Iam planning on attaching this script to many objects(over 100 objects in one level).

    What iam having trouble now is:
    How can i make "PopUp" Object pop up when player get closer. If diffrence between player and object is less then 5, object moves up. How can i do that?

    http://answers.unity3d.com/questions/536563/besy-way-to-check-distance-between-player-and-obje.html
    I found this, but in this script i have to use
    Code (CSharp):
    1. player = GameObject.FindGameObjectWithTag("Player").transform;
    If over 100 of object have my "MovingObstacle" scripts and all is trying to "FindGameObjectWithTag" wont this slow down performance. Iam developing game for mobile platform.

    am i thinking too much agian? :/
     
    Last edited: Mar 8, 2017
  11. sheepixel

    sheepixel

    Joined:
    Mar 8, 2017
    Posts:
    2
    well, maybe.
    please don't get me wrong, it's good to think about such things that will affect your players, the best thing you can do to know if something will crash your game in mobile is to try it in your own device. That's the best thing you can do to test if some part code can or can't slow down your game's performance, try to print on screen the fps rate before this code and make whatever you want and print again while runing this code and see if it works or not :)
     
    Samantha_K likes this.
  12. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    @cyberpix Thank you for your reply. I will try that :)

    Edit;
    framerate is dropping when using
    Code (CSharp):
    1. player = GameObject.FindGameObjectWithTag("Player").transform;
    so i have to find another way.

    Thank you again @cyberpix for advice.
     
    Last edited: Mar 8, 2017
  13. sheepixel

    sheepixel

    Joined:
    Mar 8, 2017
    Posts:
    2
    it is a guess, but maybe you could make it public and drag and drop the player object to the enemy inspector in your code, but i think it is basicaly the same thing you do in code, but i am not sure. It is worth it a try.
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You'll be fine if you only call FindGameObjectWithTag in the Start method, storing the result in a field on the script. It's only a problem if you call it every frame.

    Your code looks very good, by the way. I like how you have it divided into several small, simple methods rather than one big ugly method!

    I have just a couple of small style suggestions...
    1. Using "[SerializeField] private" on every field seems like working harder than you need to. I understand the arguments for it, but it's really not likely to be a problem if you simply declare any field as "public" if you want it to appear in the Inspector, and as nothing (implicitly private) otherwise. Yes, this means that some other script could in theory access those properties — but it'd have to get a reference to it first, which is hard enough that you're not too likely to do it thoughtlessly. So I say, keep the code simple unless you find it's actually a problem.
    2. When checking boolean flags, you don't need to say "if (something == true)". Just say "if (something)" instead. It means the same thing, and is shorter (and I think clearer, but I guess opinions may vary on that).
    Of course you can develop your own style! These are just some ideas to maybe make writing the code easier.
     
  15. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    291
    Any Find method to get references to gameobjects should be called as sparingly as possible, since they have to iterate through the complete scene which is quite computation heavy for any non-trivial scene.

    If you want an object to react to the proximity to another object, the easiest method would be Triggers. Add a collider to the object and add a OnTriggerEnter function. Inside this function you can test, if the object is the right type of object and then react to it. Something like this:

    Code (CSharp):
    1. private void OnTriggerEnter(Collider other) {
    2.     if(other.gameObject.CompareTag("Player")) {
    3.         //do stuff
    4.     }
    5. }
    You can use OnTriggerExit to react to an object leaving the collider, or OnTriggerStay to act every frame as long as the object intersects the collider.

    Alternatively you could write a function to check your surroundings:

    Code (CSharp):
    1. private Collider[] CheckProximity(float radius) {
    2.     return Physics.OverlapSphere(transform.position, radius);
    3. }
    You can cll this function periodically, either via Update, a Coroutine, or InvokeRepeating:

    Code (CSharp):
    1. private IEnumerator CheckProximityCoroutine(float radius, float timeBetweenRepetitions) {
    2.     while(true) {
    3.         Collider[] objectsInProximity = CheckProximity(radius);
    4.         //do stuff with the objects...this could also be done inside the ChekProximity function instead of just returning the objects
    5.         yield return new WaitForSeconds(timeBetweenRepetitions);
    6.     }
    7. }
    Don't listen to that...I was about to compliment you on good practice of keeping private stuff private. [SerializeField] is the correct way of handling these variables.
     
    Last edited: Mar 8, 2017
  16. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    291
    Forgot to mention: I personally would place this behaviour on the Player, and not on the Objects that should react, so it is called only once, and only objects close to the player have to be checked. You can then call a function on the other object for the reaction. If you want to make it really clean, create an Interface that declares that function. Different objects can then implement that interface and react to the player's proximity.

    Code (CSharp):
    1. public interface IReactor {
    2.    public void ReactTo(Player player);
    3. }
    This would then be in the Player script, where you check the objects in the Player's proximity:
    Code (CSharp):
    1. IReactor reactor = other.GetComponent<IReactor>();
    2. if(reactor != null) {
    3.     reactor.ReactTo(this);
    4. }
     
    Samantha_K likes this.
  17. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Don't listen to Piflik. ;) He's advocating practices which are good in big complex projects, but which are not the simplest or most beginner-friendly solutions, and will increase your risk of never getting the darn thing to work.

    In addition, from a philosophical point of view, it shouldn't be the player object's responsibility to tell other objects when it gets close — better to put that behavior in the objects themselves, as you (Samantha) already have it. And you can easily do that by getting a reference to the player (in Awake or Start), and then simply checking the distance with Vector3.Distance.

    (Or if it's the case in your game that only the Y position matters, then you can just check Mathf.Abs(player.transform.y - transform.y)... I don't know if that's the case here or not, but it might be.)

    One way to tell that this is the "right" design is to ask: what if some objects need to react at a different distance than others? Or if some need to react based on distance, while others react based on Y position, and still others based only on X? Having the player object cast a sphere and notify nearby objects will not work (or at best, will get really messy) under these conditions. But having each object manage its own sensing and reactions, keeps it clean and easy.

    But, as @Piflik and I have demonstrated, there is more than one way to skin a cat. Do whatever seems simplest to you and don't worry too much about what anybody else says (including me!).
     
    Samantha_K and Dave-Carlile like this.
  18. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Pretty much this. There is no one "right" way to do these things. Whatever gets the job done is all that matters. You can spend all of your time trying to apply so-called "best practices" to your code design and never get anywhere with your project because in many cases it complicates things more than they need to be. As I said earlier, write the code to get it done, and once it's done you can go back and polish things up if you wish.
     
    Samantha_K, Kiwasi and JoeStrout like this.
  19. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi all, Wow Unity community is awesome, Thank you all for super awesome support. Thank you @cyberpix @JoeStrout @Piflik and @Dave-Carlile.

    With your support i manage to crate this;
    for me this is working perfect.
    Code (CSharp):
    1.  
    2. void DistanceCheck(float radius){
    3.         if (wakeUpInDistance) {
    4.             myCollider = Physics.OverlapSphere (this.transform.position, radius,mask);
    5.             foreach (Collider col in myCollider) {
    6.                 if (col.gameObject.CompareTag ("Player")) {
    7.                     isPlayerNearby = true;
    8.                 }
    9.             }
    10.         }
    11.     }
    12.  
    13.     void OnDrawGizmosSelected(){
    14.         Gizmos.color = Color.yellow;
    15.         Gizmos.DrawWireSphere (transform.position, wakeUpInn);
    16.     }
    But i also found out that i can do this;
    Code (CSharp):
    1. public class Distance : MonoBehaviour {
    2.  
    3.     private Transform t;
    4.     private Transform player;
    5.  
    6.     public
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         t = this.transform;
    11.         player = GameObject.FindGameObjectWithTag("Player").transform;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (player) {
    17.             float dist = Vector3.Distance(player.position, transform.position);
    18.  
    19.         }
    20.     }
    21. }
    22.  
    Earlier when i tried this Vector3.Distace method, i had this in Update
    Code (CSharp):
    1.     void Update () {
    2.         if (player) {
    3.             float dist = Vector3.Distance(player.position, transform.position);
    4.             print ("Player distance is " + dist);
    5.         }
    6.     }
    "print" was the reason for framerate dropping. :rolleyes:
    Just like you guys are saying
    ;);)

    Anyway so both method is working fine. but iam going to use Physics.OverlapSphere for now.

    THank you all for awesome support and i will keep update this thread. Feel free to give more feedback.
     
  20. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oh yeah! We should have pointed that out — print (or Debug.Log) will really slow your game down if it happens every frame. But it's a good habit to get object references (like your player field) in Awake or Start anyway.

    Great job getting this working. Keep it up! :)
     
    Samantha_K and Dave-Carlile like this.
  21. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    291
    Yeah, printing to the console is tanking the framerate. I still sometimes wonder where my performance goes and then find the console output via the profiler...

    I'm glad it works, but I have to criticize my own advice and say that the overlap sphere is really only then a performant solution, if you put the code in the player script. If it is done in the moving Object, this code checks each object's surrounding area and checks each found object's tag, even if none of them is the player. OnTriggerEnter would be functionally identical to this, but the code is only called when an object actually enters the collider. It also offers a bit more freedom, since you can use differently shaped colliders and not only a sphere.

    If you want to leave the code on the moving object, I think the direct reference to the player-object is better. If you don't want to explicitly get the reference to the player in every object, you might want to consider a Singleton pattern. This allows access to the player from anywhere in your code and enforces that there is always exactly one single instance of the player. Many people (myself included) advise against using Singletons, though, unless really necessary. But as long as you don't want to add multiplayer, it should not pose any big problems. The following is a very simple alternative. Not especially fancy (or safe), but it is more or less a modified (and extremely simplified) version of the Service Locator pattern. You can turn this into a Singleton by checking if the instance already exists and destroying the object in that case, instead of registering itself.

    Code (CSharp):
    1. //put all of this in the Player class
    2. public static Player instance {get; private set;}
    3.  
    4. void Awake() {
    5.     //uncomment to turn into Singleton:
    6.     //if(instance != null && instance != this) {
    7.     //  Destroy(gameObject);
    8.     //} else {
    9.     instance = this;
    10.     //}
    11. }
    But: As long as you don't have performance issues (on the device, not in editor) you don't need to change this. It might be something to keep in mind for later, hoewever.
     
    Samantha_K likes this.
  22. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi again, sorry i have been very busy, study and stuff :( i wish i had more time do what i love> Developing games :D. Thank you very much @JoeStrout and @Piflik for your all love and support :).

    I think i have gameplay done for now. i am going to create many levels and test how gameplay is.
    Again i cant Thank you all enough. This is first time after almost 2 years of learning C# and unity i have come so far as having something to test :D. i Love you.
     
    JoeStrout likes this.
  23. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Sounds like you've made some real progress! Feel free to show off your work in the Works in Progress forum. Screenshots and videos are always fun to see.
     
    Dave-Carlile likes this.
  24. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi, iam struggling to make player jump while moving forward. can anyone help me this one to.
    I want player to jump over red obstacles. Player moveforward speed can increase over time, but i dont want that to affect jump height and jump length.

    Please Thank you
     

    Attached Files:

  25. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    I can't look at Unity right now, but I want to verify what you're asking. You always want the length and height of the jump to be the same, even though the forward speed is faster? The only way to keep those two variables the same would be to decrease the amount of time the jump takes.

    For example, if moving at speed X the jump takes Y seconds, when moving at 2X the jump would have to take Y / 2 seconds. In other words, when moving twice as fast the jump would have to take half as long to cover the same height and distance.

    I think you'll have trouble doing that with the physics system since it doesn't really obey normal physical laws. I suppose you might be able to do something with changing mass and force to get it to work but that's beyond my Wednesday morning ability to think through.

    So basically you have a parabola of sorts, and you have to move the player from the beginning to the end of the parabola in the available time. That can be done with an interpolation function where you move t from 0 to 1 over the course of Y. There is probably some relatively simple math involved there, or some of the tweening libraries in the asset store can probably help.

    Anyway, you seem to be someone who can work things out from basic info so hopefully this is enough to point you in the right direction. It's also certainly possible that I'm missing something very simple, but others will be sure to correct me and we can both learn something. :)
     
    Samantha_K likes this.
  26. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    @Dave-Carlile Thank you so much for your reply.

    Yes!. But i realised that won't be easy with unity physic engine. I have to create my own physics system. Iam not so good with math or physic :(. Anyway i am going to try it.
    If i dont use unity physic engine, player wont be able to push away obstacles with rigidedbody attached. Iam right?


    Here is my code on Player now: its mess but. Iam still learning. :D
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     [SerializeField] private float ballSpeed;
    7.     [SerializeField] private float forwardSpeed;
    8.     [SerializeField] private float forwardSpeedAcceleration;
    9.     [SerializeField] private float maxForwardSpeed;
    10.  
    11.     //Jump Test
    12.     [SerializeField] private int jump;
    13.     [SerializeField] private float jumpForce = 2.0f;
    14.     [SerializeField] private bool isGrounded;
    15.     //Jump Test end
    16.  
    17.     private Rigidbody myRB;
    18.     private Vector3 screenPoint;
    19.     private Vector3 offset;
    20.     void Awake(){
    21.         myRB = GetComponent<Rigidbody> ();
    22.     }
    23.  
    24.     void Update(){
    25.         MouseMove ();
    26.         ForwardSpeedIncrese();
    27.     }
    28.  
    29.     void FixedUpdate(){
    30.         Jump();
    31.         Vector3 moveForward = (Vector3.forward*forwardSpeed);
    32.         Vector3 movement = new Vector3 (this.transform.position.x, 0, moveForward.z);
    33.         myRB.velocity = new Vector3(0, myRB.velocity.y, movement.z * Time.deltaTime);
    34.     }
    35.  
    36.     void MouseMove(){
    37.         if (Input.GetButtonDown ("Fire1")) {
    38.             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    39.             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    40.         }
    41.  
    42.         if (Input.GetButton ("Fire1")) {
    43.             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    44.             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    45.             transform.position = new Vector3 (curPosition.x, this.transform.position.y, this.transform.position.z);
    46.         }
    47.     }
    48.  
    49.     //Jump Test
    50.     void OnCollisionStay(Collision collision)
    51.     {
    52.  
    53.         isGrounded = true;
    54.     }
    55.  
    56.     void Jump()
    57.     {
    58.         if (Input.GetKey(KeyCode.Space) && isGrounded)
    59.         {
    60.             Vector3 tempJump = new Vector3(myRB.velocity.x, jump, myRB.velocity.z);
    61.             myRB.AddForce(tempJump * jumpForce, ForceMode.Impulse);
    62.             isGrounded = false;
    63.         }
    64.     }
    65.  
    66.     void ForwardSpeedIncrese()
    67.     {
    68.         if (forwardSpeed < maxForwardSpeed)
    69.         {
    70.             forwardSpeed += forwardSpeedAcceleration * Time.deltaTime;
    71.  
    72.         }
    73.         else
    74.         {
    75.             forwardSpeed = maxForwardSpeed;
    76.         }
    77.  
    78.     }
    79. }
    I have to say i really love unity community. You people are sooo helpful, THANK YOU
     
    Last edited: Mar 22, 2017
    JoeStrout likes this.
  27. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That code's looking pretty good, Samantha! I can tell you've learned a lot since you first began.

    Yes, if you let the physics engine do your jumps (via Rigidbody.AddForce), you have to give up having fine control over exactly what path your jump takes. That's kind of the whole point of letting the physics engine move things for you: it moves them how it thinks they should move, so you don't have to.

    But note that if you do the physics yourself (which is easier than you probably think, in most cases), you can still shove around other rigidbodies. It's just that instead of setting your velocity and using AddForce, you'd instead move your player on each frame with Rigidbody.MovePosition.

    However, in this case, I suspect you can get away with relying on the physics engine for your movement a while longer. Perhaps give this a try:
    1. When you jump, instead of using AddForce, try setting myRB.velocity directly. You'll need to experiment with exactly what this "launch" velocity should be, but it should be upward and forward of course.
    2. In your FixedUpdate, don't muck with the velocity there unless isGrounded is true.
    While you're at it, you might rethink that FixedUpdate code... I can see it has some cruft left over from previous work. Consider:

    Code (CSharp):
    1.         Vector3 moveForward = (Vector3.forward*forwardSpeed);
    2.         Vector3 movement = new Vector3 (this.transform.position.x, 0, moveForward.z);
    3.         myRB.velocity = new Vector3(0, myRB.velocity.y, movement.z * Time.deltaTime);
    Line 1 creates a vector (0, 0, forwardSpeed). Line 2 makes another vector that's the same, but with the x set to your current position, like (x, 0, forwardSpeed). And then line 3 ignores that x, and just pulls out the Z part... which is to say, forward Speed... and sets your velocity to (0, myRB.velocity.y, forwardSpeed * Time.deltaTime).

    So you could replace all that with

    Code (CSharp):
    1.     if (isGrounded) {
    2.         myRB.velocity = new Vector3(0, myRB.velocity.y, forwardSpeed * Time.deltaTime);
    3.     }
    But now that we look at it, this doesn't make much sense from a physics perspective. If forwardSpeed were actually a speed, then you should use it directly for your velocity, without multiplying by Time.deltaTime. The way you're using it, you're treating it kind of like an acceleration... except that it's not at acceleration if you reset your velocity on every frame.

    So while you're at it, you should take out that "* Time.deltaTime" factor, and adjust your forwardSpeed value so that it's the actual speed you want (probably about 50 times higher than the current value).
     
  28. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    This is the part I'm having a problem with, but maybe I'm just overthinking. If the desire is to have the length and the height of the jump to be the same, then the amount of time the jump takes must be shorter if the forward speed is higher.

    But thinking through it some more I guess the angle of the jump could change too and produce the same result. But that would be the same as reducing the forward speed during the jump. And actually that ends up as just setting the forward speed to the initial speed during the jump, and restoring it on landing. Not sure how that would look or if it's the desired behavior.
     
  29. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    But the forward speed isn't higher with the method I propose. We're clobbering whatever forward speed we may have had, and setting a specific velocity at the start of the jump. (And then changing FixedUpdate so that it doesn't overwrite this velocity until we're grounded again.)

    So, exact same velocity every time, ought to mean same length and height of jump every time, unless I'm missing something.
     
  30. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    I think we're saying the same thing now - my brain just came at it from "how must the jump angle be changed to produce a jump of the same length no matter the forward velocity". But yeah, the required angle always ends up always producing the same velocity vector so it's irrelevant.

    So @Samantha_K, ignore everything I said and do what Joe said :)
     
  31. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Hi, @JoeStrout Yes i have learned a lot since my first post here in this thread. REALLY THANKS TO YOU. I was worrying about ObjectPooling when i even cant move the ball. Heheh. So thank you for your great advice. Thanks everyone.

    Ball jump is working, Thanks @JoeStrout, but i have little problem with “DoubleTap” , ball won’t always jump. I have not created touch script yet. I don’t know how i can “translate” this to One Touch control with exact move control.
    Code (CSharp):
    1.  
    2.  
    3. private int jump = 5;
    4.  
    5. private float lastClickTime;
    6. private float catchTime = 0.25f;
    7.  
    8. void MouseMove(){
    9.  
    10.         if (Input.GetButtonDown("Fire1"))
    11.         {
    12.             if (Time.time - lastClickTime<catchTime)
    13.             {
    14.                 JumpDoubleClick();
    15.             }
    16.             else
    17.             {
    18.                 screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    19.                 offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    20.             }
    21.             lastClickTime = Time.time;
    22.         }
    23.    
    24.         if (Input.GetButton ("Fire1")) {
    25.             Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    26.             Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    27.             //transform.position = new Vector3 (curPosition.x, this.transform.position.y, this.transform.position.z);
    28.             //transform.position = Vector3.Lerp(this.transform.position, new Vector3(curPosition.x, this.transform.position.y, this.transform.position.z), 0.2f);
    29.             transform.position = Vector3.SmoothDamp(this.transform.position, new Vector3(curPosition.x, this.transform.position.y, this.transform.position.z), ref velocity, movementSmoth);
    30.         }
    31.     }
    Code (CSharp):
    1. void JumpDoubleClick()
    2.     {
    3.         if (isGrounded)
    4.         {
    5.             myRB.velocity = new Vector3(myRB.velocity.x, jump, myRB.velocity.z);
    6.             isGrounded = false;
    7.         }
    8.     }
    But will work on that later. I just want to share what i have done so far and how my project is going.


    About Objectpooling:
    -i tested on mobile using Instantiate and Destroy method with only 10 “Track”. Since my plan is to have almost 100 “Track”, some track contains physics object, animation object, moving object. So i want to try Object pooling method. I spent many days trying to create object pooling system., I watched many tutorials about object pooling, but all tutorials is about pooling one object “bullet”. What i wanted was multiple object pooling system and spawn in random order. After many days i created just like what i wanted. Learned about List and more, i was soo happy and game got performance boost with 10-20 frame rate. But yet again i was facing difficulty, “Track” is saved as parent prefabs, and parent prefabs contain many child object with movingObstacle script. When object spawn for first time all is good, but when object spawn second time child object are “done” with moving. I wanted to set them back to original state. So i spend more days on object pooling, after searching on the internett i found this;
    https://www.reddit.com/r/Unity3D/comments/36q5ej/object_pooling_with_complex_gameobjects_nested/

    But this was too complicated for me now, so i went back to Instantiate and Destroy method, so far it's working but i tested only with 10 tracks, i don't know how this will affect on performance when i have 100 tracks to instantiated and destroyed,
    I created variable max allowed trackToSpawn = 6, so always there will be only 6 track in scene. I hope this works, Time will tell :D


    About Graphics, Lightmapp
    Looks like graphic is my biggest challenge right now. I have created Tracks in maya, UV layout in maya. 3d assets are fairly simple and very very low poly. Mostly box type assets.

    This is what i have tired:
    Lightmap baking in Unity(5.5.2p2)
    • -3d assets has UV created in maya
    • -In import Setting>Generate Lightmap UVs is checked.
    • -Lightmap static in Inspector to all static 3d assets and Directional Light
    • -Directional Light>baking>mixed
    • -Lighting setting window looks like this

    Problem iam having with this method is:
    • - cant save baked lightmapp to prefabs
    • -”seam” line is very visible on next track start and end.
    • -I get a lot of “compression artifact” when using “Compressed” in Baked GI in lighting settings window, So i don’t want to use it, so file size is 200mb with only 10 tracks, but apk size 30mb So what happen when i have 100 tracks.

    I have read tru this thread,
    https://forum.unity3d.com/threads/problems-with-instantiating-baked-prefabs.324514/
    but i don't understand it. Why is this so difficult. What do you all do for better mobile graphic?

    Sorry for very long post, Iam so excited, after many months with trying failing never come so far. Thank you all the support
     

    Attached Files:

    Last edited: Mar 26, 2017
    JoeStrout likes this.
  32. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hey Samantha, good to hear you're making progress! I'm afraid I don't know much about light mapping; I've only poked at it a little and never used it for a real project. Maybe someone else will be able to help.
     
  33. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Its ok @JoeStrout, if you have time can you please take a look at this thread, I think iam going to turn off shadow for this game until i learn more about it. But for now i struggle with control :(