• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Runemark Studio
      Dark Fantasy Environment and Props





    • By bowcox
      Hi guys! 
      I have created a Pong game that has an AI that is almost beatable, changing the speed of the AI can make it ridiculously easy or hard depending on the way you go about it. 
       
      using System.Collections; using System.Collections.Generic; using UnityEngine; public class ComputerMovement : MonoBehaviour { private float speed; private float reAdjustSpeed = 1f; private Rigidbody2D computer2d; public static bool isTwoPlayer; GameObject theBall; Rigidbody2D rb2d; void Start() { computer2d = GetComponent<Rigidbody2D> (); } void FixedUpdate() { if (isTwoPlayer == true) { speed = 5f; if (Input.GetKey (KeyCode.W)) { computer2d.position += Vector2.up * speed * Time.deltaTime; } if (Input.GetKey (KeyCode.S)) { computer2d.position += Vector2.down * speed * Time.deltaTime; } } if (isTwoPlayer == false) { speed = 3f; if (theBall == null) { theBall = GameObject.FindGameObjectWithTag ("Ball"); } rb2d = theBall.GetComponent<Rigidbody2D> (); //Is the ball going left or right if (rb2d.velocity.x > 0) { if (rb2d.velocity.y > 0) { if (rb2d.position.y > computer2d.position.y) { MoveUp (); } if (rb2d.position.y < computer2d.position.y) { MoveDown (); } } if (rb2d.velocity.y < 0) { if (rb2d.position.y > computer2d.position.y) { MoveUp (); } if (rb2d.position.y < computer2d.position.y) { MoveDown (); } } } //Whilst it's not moving at the paddle, let it gain a slight reset by moving with the ball at a slower pace. if (rb2d.velocity.x < 0) { if (computer2d.position.y < 0) { computer2d.position += Vector2.up * reAdjustSpeed * Time.deltaTime; } if (computer2d.position.y > 0) { computer2d.position += Vector2.down * reAdjustSpeed * Time.deltaTime; } } } } void MoveDown() { if (Mathf.Abs(rb2d.velocity.y) > speed) { computer2d.position += Vector2.down * speed * Time.deltaTime; } else { computer2d.position += Vector2.down * speed * Time.deltaTime; } } void MoveUp() { if (Mathf.Abs (rb2d.velocity.y) > speed) { computer2d.position += Vector2.up * speed * Time.deltaTime; } else { computer2d.position += Vector2.up * speed * Time.deltaTime; } } }  
      I have looked up several posts across many different forums in order to create a much better AI. Most of the posts recommend that I use Raycasts to find out exactly where the ball might hit the paddle. I have looked up how to use them and I'm just completely lost, do raycasts consider collisions and go on infinitely or once they hit a wall, that's where it'll end up? Would anyone be able to help me understand raycasts a little better? 
      If you have another solution that enables me to calculate exactly where the ball will end up on the opponents side, I am more than willing to hear it
      Thanks again if you read this!
    • By eldwin11929
      We're looking for a Unity (C#) Programmer for our 2D Project. We're looking for a new lead programmer to continue with an existing project.
       
      Project is an open-world RTS, and is very close to a prototyping (playable) phase. Our existing lead, unfortunately, has no more time for the project, and thus we are in search of a new one who is interested.
       
      Game is purely fantasy based, and we'll be able to give you much more detailed info about the project as we begin to work you into it.
       
      You'll be working with our junior developer, who has been here since the beginning.
       
      Primary skills needed are just being able to work within Unity. But skills within XML are also a plus.
       
      Our list of major goals we'd need you to do is minimal, yet still fairly extensive:
      -Edit our current Pathfinding system to account for a few extra features.
      -Setup our global map system. You’ll be working off an existing random node-map web generator and existing random map generation system and essentially linking the two together. This includes handling random spawns (which has already been semi-setup) unique to each node’s respective map.
      -Further implementation of an existing random spawning algorithm (used to spawn things like enemies randomly across the Map).
      -Making sure to Save and Record all respective aspects of what was mentioned above.
      -Handling our XML database- database is created, but we’ll need to be integrating it. This includes all various things from units to abilities and so forth. Will also need to handle implementing an object’s unique attributes we cannot take care of within XML.
      -Various Content Implementation (to be done once our XML has been integrated).
      -Various Saving and Recording of all respective aspects of the database info mentioned above.
      -Various Performance Enhancements.
      -Potential for various misc things, such as further UI work.
      -Setting up a Menu system.
       
      We have a considerable amount of things done already- however I must warn ahead of time we have quite a bit of unclean code, which may be fairly overwhelming for a new developer on the project.
       
      Let me know your rates per hour, and we'll see if we can work out a good deal between both of us. Royalties are also included.
       
      If interested, send an email to: eldwin11929@yahoo.com
       
      Thanks!
    • By jhocking
      My bestselling and highly recommended Unity book has been fully revised! Unity in Action, Second Edition teaches you to write and deploy games with the Unity game development platform. You'll master the Unity toolset from the ground up, adding the skills you need to go from application coder to game developer.

      Foreword by Jesse Schell, author of The Art of Game Design

      Don't take my word for it being good, look at the sky-high ratings on GoodReads.

      You can order the ebook directly from the publisher's site, or order the book on Amazon to get both the physical book and a coupon to download the ebook!
    • By ThunderTwonk
      Hello everyone, I am working on a game idea and since I am still in the process of learning C# and the features available in unity I was hoping some of you might be able to offer me a little insight on things in general for getting started.
      I guess the basic components of what I'm wanting to create would be a Multi-levels management/city builder/rpg.
      The goal is to provide a framework for players to interact with, build in and affect the world both from a 3rd person action RPG as well as a zoomed out 4x style view (This would be something unlocked through gameplay)
       
      As for my questions go I was wondering if anyone had resources that could help me learn.  I've been on youtube as well as enrolled in an online course for basic unity and C# and will continue those but if anyone has any words of advice, a place that has good information and tutorials etc.
       
      Thanks for your time.
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Need help programming car game

This topic is 475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I'm using Unity. I'm making a really sick car game, with dynamic car damage, crazy stunts, and wacky cartoonish graphics. I'm running into problems with math, though. Basically, I'm trying to make a function that inputs the damage done to the car and the Vector3 location of the impact. Then, the code should use this information to distort the mesh around the point of impact by a reasonable amount.

 

It looks like the following in pseudocode:

public void DestortMesh(collision point, amount of damage)
{
    Radius = damage / 10;
    for (each verex of the mesh)
    {
        if (the vertex is within the radius from the collision point)
        {
              distort the vertex
        }
    }
}

Here is the actual code:

    void deform(float radius, ContactPoint contactPoint)
    {
        Debug.Log("Deform called with radius of " + radius);
        Vector3 relativePosition = gameObject.transform.InverseTransformPoint(contactPoint.point);
        for (int x = 0; x < vertices.Count; x ++)
        {
            Debug.Log(Vector3.Distance(vertices[x], relativePosition));
            if (Vector3.Distance(vertices[x], relativePosition) <= radius)
            {
                //time to deform
                vertices[x] += vertices[x] * Random.Range(-.1f,.1f);
            }
            
        }

        mesh.SetVertices(vertices);
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }

This looks all fine and dandy, right? but when I look at the distances between each vertex and the collision point (the debug log right before the if statement), they look like this:

 

f9738bbdd33a7aa654b4c85a9fd3dd0d.png

 

basically, the output is weird. The speed of impact seems to affect it slightly, and for every impact, each vertex's distance is within a few 1000ths of each other. This makes it really hard to tell which points are close and which ones are far from the impact point. When trying out this in the game, the damage to the car is sporadic. For the most part, no damage is visible on the car. Then randomly, usually after a high speed impact is made, the car will become completely distorted, with every vertex changed.

 

You can see a video of this happening here:

 

So in the end, this is not the result I was aiming for. Something seems to be off with the Vector3 distance, but in the Unity forums all I'm getting are people saying there couldn't possibly be anything wrong with them. So I'm going to assume that there is something wrong with my code perhaps?

 

If you think you could help me, add me on Discord. Ramaraunt#6934

Edited by Ramaraunt

Share this post


Link to post
Share on other sites
Advertisement

Several things:

 

a) What is the ratio of the size of the bounding box of the mesh (enough "of" for now ;)) and the damage infliction?

 

b) From the video it seems me that the mesh has a too low resolution for deformation. Either use a higher resolution in advance or refine resolution locally just before applying the damage.

 

c) Damage infliction is done by applying a translation in direction of and proportional to the position of the vertex. That means that the intentional same damage factor applied to a vertex farther away from the origin has a higher impact, and that the bumps are not oriented correctly. What you probably want is to support a direction of damage (i.e. the damage is input as a vector with its length denoting the damage amplitude), scale that with one minus the normalized distance from the impact point, and add that to the vertex position.

 

d) Using a translation in range [-1,+1] gives inside and outside bumps; perhaps not what you want.

Share this post


Link to post
Share on other sites

Several things:

 

a) What is the ratio of the size of the bounding box of the mesh (enough "of" for now ;)) and the damage infliction?

 

b) From the video it seems me that the mesh has a too low resolution for deformation. Either use a higher resolution in advance or refine resolution locally just before applying the damage.

 

c) Damage infliction is done by applying a translation in direction of and proportional to the position of the vertex. That means that the intentional same damage factor applied to a vertex farther away from the origin has a higher impact, and that the bumps are not oriented correctly. What you probably want is to support a direction of damage (i.e. the damage is input as a vector with its length denoting the damage amplitude), scale that with one minus the normalized distance from the impact point, and add that to the vertex position.

 

d) Using a translation in range [-1,+1] gives inside and outside bumps; perhaps not what you want.

A: There will be 100 damage maximum before the car is wasted. The bounding box is about 2 by .4 by .7.

B: I want the resolution to be low. Is there a way to make that work?

C: Normalized distance? How do you normalize a float?

D: Yeah that could be changed.

 

 

I'll pm you  a copy of the game.

 

EDIT: I tried this

vertices[x] += Vector3.Normalize(vertices[x] - relativePosition) * Random.Range(-.1f,.1f); 

and it does this

 

d94fff0b72e0cefcde54479e3adb3d26.png

Edited by Ramaraunt

Share this post


Link to post
Share on other sites

BTW: I'm *not* running Unity3D at the moment, so we need to solve the problem on a more theoretical basis.

 

to A: What I asked for is the ratio of the bounding box size to the area of damage. If the radius of the sphere that locates the damage is, say, 1.0, while the bounding is 2 by .4 by .7, then every applied damage has an influence on at least half of the vertices. Hence my question: Is the ratio low enough?

 

to B: Think of the radius being 0.3 when damage is inflicted to the front right corner of the car. The bounding box's size in x direction is 2, and it is covered by 2 faces along from front to back. Only the vertices at the front are located within the sphere of damage. What we would expect is that the region 0.7 to 1.0 along x direction is damaged. However, the mesh's face ranges from 0.0 to 1.0 in that direction, and hence the deformation appears over half of the length of the car although the relatively small damage sphere.

 

to C: "Normalized distance" means that it ranges from 0.0 to 1.0. Since the unnormalized value ranges from 0.0 to radius, dividing by radius is the calculation to be done for normalization.

 

to D: Well, when being done "correctly", then this kind of calculation will be removed totally.

 

 

It seems me worth to clarify first how exactly an inflicted damage should look like...

Edited by haegarr

Share this post


Link to post
Share on other sites

I appreciate the responses! 

 

Here is my fixed up code using what you said

    void deform(float radius, ContactPoint contactPoint)
    {
        Debug.Log("Deform called with radius of " + radius);
        Vector3 center = gameObject.GetComponent<Rigidbody>().centerOfMass;
        Vector3 relativePosition = gameObject.transform.InverseTransformPoint(contactPoint.point);
        for (int x = 0; x < vertices.Count; x ++)
        {
            float normalized = (1 - (Vector3.Distance(vertices[x], relativePosition) / radius));
            //Debug.Log(Vector3.Distance(vertices[x], relativePosition));
            if (normalized > 0)
            {
                //time to deform
                vertices[x] -= vertices[x] * normalized *Random.Range(1, 5);
            }

            Debug.Log(normalized);
            
        }

        mesh.SetVertices(vertices);
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }

Here is the debug log output. Its still weird though.

 

http://pastebin.com/dH5fGy3Y

 

Why are all the distances for each point equal to about -3?

 

EDIT: btw, I've got a friend from blenderartists.org who is pumping out some nice models. In light of this, I changed the shaders to be more smooth looking. I like it much better.

 

48dad61308304039fb2876f5b886232b.png

Edited by Ramaraunt

Share this post


Link to post
Share on other sites

So, I tried doing just this:

float normalized = ((Vector3.Distance(vertices[x], relativePosition)));

And the distance between each vertex and the collision point is .98XXXXXX every single time.

Share this post


Link to post
Share on other sites

Solved it! The problem was, the model was originally imported really tiny, and it was scaled up 100 times. So the mesh.vertices values were really tiny.

 

Here is a video of the fixed game!

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement