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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make a gameobject smoothly align itself after the surface below it?

Discussion in 'Scripting' started by bengtjan, May 10, 2017.

  1. bengtjan

    bengtjan

    Joined:
    May 7, 2017
    Posts:
    2
    Disclaimer: Im not good at programming in c#, im still learning as i go and this is also my first time in this forum. Tried the "answers" forum but never got any answers so i'll thought i might try here!

    Okay, so i've looked all over for a solution for my problem and i can't seem to find something that fits my needs. Im trying to make a skateboard game (done some smaller, more simple stuff before but wanted to challenge myself a bit) and it's been going OK so far but now i hit my first big obstacle.

    If you take a look at the video-link below you can see that every time my player jumps the ground and the different forces when you jump makes it so that the player sometimes lands in a weird angle. How do i make it so that the player/board-gameobject smoothly aligns itself after the surface below it? Like a "magnet" that helps the player to even itself out before landing?

    Video-link:
     
  2. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Despite how simple it may look, this is a really complicated problem to get right. Ideally I'd go with a physics driven system based on a complicated animation controller on your character. However, this is really really hard to get right.
    So here's a light version :

    - The player starts to look janky once 1 of the 4 wheels touches the ground far earlier than the rest. So let's try to create a balance based on the wheels only.
    - Calculate the velocity of each wheel every frame.
    - Use Raycast to find the estimated collision point with the ground over the next N frames. Keep gravity in mind. This is just an estimate since animations will probably mess it up, but it's something.
    - Calculate time taken until it hits the ground for each wheel.
    - Let's name the wheels in clockwise order '1,'2,'3,'4
    - wheels that will soon hit the ground should be slowed down, while other wheels should move faster. You can do this via rotation. Eg if '1 will hit the ground in 1 time unit, and '3 in 2 time units. then you can rotate the board via the axis between '2 and '4 via a certain degree which you can calculate via pythagoras' triangle equations , based on the difference in time left between '1 and '3.
    - You can now use Math.Lerp to smoothly interpolate between the previous rotation and the desired one.
    - Additional checks can be done between all adjacent pairs of wheels too.

    Having written this, this is probably a fairly mediocre solution. A skateboarding game probably uses its' own physics engine to simulate the behaviour of the board and player more realistically. It's also perhaps worth checking out Neural Networks, they might be able to keep balance based on some simplified input of the immediate environment.

    All in all, it's not that nobody here or on Unity Answers wants to help you, it's just that this is a project that will take weeks if not months to get right :S .

    PS : Much simpler yet crappier solution is to Lerp the transform.down/up vector equal to the ground's normal vector. Don't know how your setup works, but if you're working with rigidbodies some light application of force may do the trick
     
    Last edited: May 10, 2017
  3. bengtjan

    bengtjan

    Joined:
    May 7, 2017
    Posts:
    2
    Well, if was fun while it lasted. Haha, joking aside. This might be too big of a step for me right now (and i'll probably should try out some other stuff in between to increase my skills before i come back to this). But i appreciate the answer to my question and i hope other people that reads this thread finds it helpful aswell!

    Thanks!
     
  4. monodokimes

    monodokimes

    Joined:
    Feb 2, 2016
    Posts:
    24
    Don't lead him don't that rabbithole! He'll be gone for weeks!