Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make My Car moving to a random Map point

Discussion in 'Scripting' started by Feretro, Jul 30, 2021.

  1. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hello!
    I'd like to move a Car (I downloaded for free in the assets) in a very simple 3D Map (Terrain) witch has some "hills" .. It's not flat!

    I can't achieve this (simple?) task! :-(

    In the awake I set two random floats for the location point on the map (x and z in the map). Of course my car should not fly! So the Y must follow the terrain inclinations!

    I tried with Character Controller but I don't know how to let my car stay on the floor (I watched "gravity" tutorial but car seem to "sink" with wheels into the terrain grass.

    Now I am trying with Rigidbody and Gravity, but Car just stands on his back bouncing around! :-O (wheelie)

    I used two capsule colliders for the read and front wheels and another capsule for the trigger on the center of my car.

    In the awake I have:

    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         speed = 1.5f;
    4.         float x = Random.Range(5f, 90f);
    5.         float z = Random.Range(5f, 90f);
    6.         float y = transform.position.y;
    7.         dest = new Vector3(x,y,z);
    8.         Debug.Log("dest: " + dest + "io sono a: " + transform.position);
    9.         arrived = false;
    10.     }
    With a FixedUpdate I call this:

    Code (CSharp):
    1.  
    2.  
    3. private void VaiVersoPlayer()
    4.     {
    5.  
    6.         myPosition = transform.position;
    7.         if (Vector3.Distance(myPosition, dest) > 2.5f && isGrounded)
    8.         {
    9.             transform.position = Vector3.MoveTowards(transform.position, dest, Time.deltaTime * speed);
    10.             transform.LookAt(dest, Vector3.back);
    11.             if (transform.rotation.eulerAngles.z != 0f)
    12.             {
    13.                 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.z);
    14.             }
    15.         }
    16.     }
    17.  
    18.  
    do you know how to make this 3D car climb up and down hills untill reach that random point?

    I was thinking about "waypoints" but ... I have to try it tomorrow!

    Thanks for you helping!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You can use raycast to find where the ground is.

    Once you are near the ground, you could lift it up perhaps 5 units, then use a terrain following thing:



    See enclosed package.
     

    Attached Files:

    maross334 likes this.
  3. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Yes! Thanks! That's what I needed! Even though raycasting is someting too advanced for me (I think!) I will study those scripts! Thanks!