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

Keep character on terrain.

Discussion in 'Scripting' started by Eacooqy, Oct 4, 2014.

  1. Eacooqy

    Eacooqy

    Joined:
    Mar 9, 2014
    Posts:
    2
    Im making a sliding game. Im trying to replicate this gameplay it starts around 2:20
    .​
    I already figured out the auto ratation and reduced any jitters on slopes to minimum:). But I can't get how to make the character stick to the ground. Now it just gets off of it when a downslope is to steep. Increasing the gravity doesn't really do the trick. Any ideas how can I achieve this?
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Can you provide more details please?

    My first guess is to go to the rigidbody component of the player (assuming it has one), go to constraints, and select the y constraint in the position field.
     
    franciscoandresparisi likes this.
  3. Eacooqy

    Eacooqy

    Joined:
    Mar 9, 2014
    Posts:
    2
    I will try my best to explain how everything is working.

    There is Object1 which follows an iTween patch and orients to it. This is the auto movement and rotation. Object2 is a child of Object3 and is responsible for side to side movement. And then the player is following Object2 (it's done with velocity changes to reduce jittering).

    I can't make it so the player won't leave the ground. You can se how its "jumping" in this video (look at the shadow).



    Unfortunately freezing the position would only work if the terrain was perfectly flat.

    Here is the player script if you need it
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class controller : MonoBehaviour {
    5.    
    6.     public GameObject obj;
    7.  
    8.     void Start()
    9.     {
    10.         obj = GameObject.Find("Object2");
    11.      
    12.     }
    13.  
    14.     public Collider col;
    15.     public float speed;
    16.     public float maxSpeed;
    17.     public float dist;
    18.    
    19.     void FixedUpdate()
    20.     {
    21.      
    22.        // Slope rotation alignment.
    23.  
    24.        RaycastHit hit;
    25.        Ray ray = new Ray(transform.position, Vector3.down);
    26.  
    27.        if (col.Raycast(ray, out hit, 1000))
    28.        {
    29.            transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
    30.  
    31.         }
    32.  
    33.  
    34.        // Auto rotation.
    35.  
    36.        transform.rotation = Quaternion.Euler(transform.eulerAngles.x, obj.transform.eulerAngles.y, transform.eulerAngles.z);
    37.  
    38.  
    39.  
    40.  
    41.        // Speed limiter which is not neceesary right now.
    42.      
    43.         /*
    44.      
    45.         if (rigidbody.velocity.magnitude > maxSpeed)
    46.         {
    47.             rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
    48.          
    49.         }
    50.         */
    51.      
    52.         // Anti jitter movement solution.
    53.  
    54.         float step = speed * Time.deltaTime;
    55.         float folX = (obj.transform.position.x - transform.position.x) * step;
    56.         float folZ = (obj.transform.position.z - transform.position.z) * step;
    57.      
    58.  
    59.         Vector3 v = rigidbody.velocity;
    60.        
    61.         v.x = folX;
    62.         v.z = folZ;
    63.      
    64.         rigidbody.velocity = v;
    65.  
    66.  
    67.      
    68.     }
     
  4. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    Vector3.MoveTowards(). ;)

    Ray- (or Sphere-) cast down to determine whether the player is grounded, and move him down a bit if necessary.
    I don't have access to my laptop right now, or I would have shared the solution from my player controller with you.

    If you'll sill need it, I can post it later.
     
  5. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    For a quick hack fix, as @TheSniperFan said ray cast down. So just raycast from the bottom of the bear to the terrain layer, then I would increase the gravity if they are above a certain height off the ground. Its probably not the best solution but it would suffice.
    another fix would be to get the vector 3 of the terrain below the bear, equal that to the vector position of the bear, with an offset. but you could run into issues with the collider on uneven slopes. To combat this you could get the rotation or gradient of the ground below and set the bears rotation to that.