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

Help with 3d Car Game.

Discussion in 'Scripting' started by tomowale, Feb 18, 2015.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Hi guys I fairly new to Unity and have been learning it for a while, but I'm currently stuck on what I need to do. My idea for my game is basically there are multiple cars coming towards you ( the player) and you can dodge and jump to avoid them , I'll add as time goes. My problem is I have NO IDEA how I should go about this so I hope you guys can direct or help me to anything that will help me build my game ( scripting , graphics, animation wise)
    Thanks
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Whew, that's a broad question sir. Can you narrow that down to a specific request? Like do you just want help with ideas for implementing the core functionality without the graphics and animations and all that (which would be my advice)
     
  3. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    First off THANKS for replying so quickly, but on topic the functionality is what I really want and need. Again Thanks.
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I would create a flat terrain and call it the road. I would then create a cube prefab and call it a car. I would then create a capsule and call it a player. Set up a script that just handles moving a player left and right. (Save jump for later. Keep it simple.) Then set up a script on the car that moves it forward at a random speed... I'd use transform.Translate, or Vector3.MoveToward, depending on how you're wanting to handle it. Put a rigidbody on both your cars and your player, and then add an OnCollisionEnter to either your car or player ... again depending on how you want to handle it.

    Create some empty game objects to act as spawn locations for your cars. Maybe out of sight down the road. Create a 3rd object in your scene that is a game or scene handler. Have that object run a loop/coroutine that spawns cars periodically. The cars should handle their own movement in their script so as soon as they spawn they should just take off and start moving. If they collide with your player, you ded.

    THAT is how and where I would start. Get that working first, and then start experimenting. If things start not going the way you want you'll have an easier time either starting over or refactoring code than if you tried introducing a bunch of complicated stuff to start.
     
    Last edited: Feb 18, 2015
  5. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Thanks man ,I'll be doing this as soon as I can.
     
  6. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I'll get back to everyone reading this on my progress , I'm trying.
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I was fidgeting around with what you said , and I wondered if my game view is supposed to look like this . I set up the terrain and the cube so far.
     

    Attached Files:

  8. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Oh. Add a directional light to your scene so you can see stuff. :)
     
  9. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    :p Thanks.
     
  10. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Update: I've widened the "road" now and added a light so you can see the "car" . Tell me if i did something wrong with the terrain. Next update will probably be me adding the player.
     

    Attached Files:

  11. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It's your game. :) Make it however you'd like to. I was simply giving you my opinion on a place to start. A first "milestone" if you will. If you like the results then I'd say you're doing great.
     
  12. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Haha thanks man, You've been helpful hopefully I can learn enough to make it into my vision of what I want it to be.
     
  13. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    992
    jtsmith1287 likes this.
  14. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    That's precisely why I started with the setup that I explained. Super simple, could be done by a beginner in a week.

    I could be starting to slip away from the mentality of "just starting" though. My idea my still be too complex. It's good to point this stuff out and I've been advocating this idea all over the forums where people are asking for advice on crazy complex ideas and showing signs that they are using this as their first project.
     
  15. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
  16. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Could just be me but I don't think the idea was all that complex ,BUT I do get where you are coming from.
     
  17. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @Fujik DAAAAAAAAAAAAAAAAAAAAAAAANGGGGGG that tutorial was helpful as hell.
     
    jtsmith1287 likes this.
  18. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I'm doing good , only having problems with the terrain.
     
    ADNCG likes this.
  19. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Update: Got player to move left and right . Did some testing Think I'm gonna make the camera follow the player.
     
  20. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
  21. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    The Unity live tutorials are pretty helpful too.
     
  22. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Update : I managed to get the player to collide with the "car" and destroy itself , only thing is Unity is telling me I have destroyed the object but am still trying to access it. If anyone can help here that would be great.
     
  23. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Is more easy if you put here your code, easy for us (forum users) to read your code and see were is the problems.
    That is, if you want.
     
  24. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.     void OnCollisionEnter(Collision collision)
    6.     {
    7.         if (collision.gameObject.name == "Player")
    8.         {
    9.             Destroy(collision.gameObject);
    10.  
    11.         }
    12.        
    13.         if (gameObject!= null) {
    14.  
    15.         }
    16.  
    17.  
    18.     }
    19.  
    20. }
    21.  
    This is the code I used to destroy the player , when it collides with the car , yet Unity says it works ,but I'm still trying to access it.
     
  25. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I don´t receive any errors. replace name to tag. And check it out.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Destroy : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter(Collision collision)
    7.     {
    8.         if (collision.gameObject.tag == "Player")
    9.         {
    10.             Destroy(collision.gameObject);          
    11.         }
    12.     }
    13. }
    14.  
     
  26. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Replaced name with tag, error gone but Object won't destroy. Oh Unity.:confused:
     
  27. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I test the code and the object whit tag "Player" is destroyed.
    Something is wrong in you scene objects. Put here your scene for view what is wrong, or screenshot in the Inspector window.
     
  28. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Good thing is it's not like `Unity won't let me test the game. So really I can just ignore this.
     
  29. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Does anyone know what a game handler , like what jtsmith said is?
     
  30. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It's an empty game object in your scene that holds a script or 3 that manages all the scene stuff. For my games there is generally a single SceneHandler object that does this. It's nothing special. Just another game object with a custom component like everything else. Except that it's sole purpose is to "manage your game". LIke spawning AI, or progressing your level or what-have-you. As you get into larger projects you'll start breaking the above functionality into smaller bits but it's a good starting point.

    Some people use a single game manager object and call DontDestroyOnLoad and use it to hold player data from scene to scene, and also manage scenes. That could get messy but really do whatever work for you and yields good results.
     
  31. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @jtsmith1287 ok I've seen it before in videos now that you explained, thanks.
     
  32. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    992
    It's probably better if you open a new thread with a relevant title and directly paste the scripts that are involved in the error.
     
  33. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @Fujik ok , probably should.