Search Unity

How do you make an object 'stick' to the terrain

Discussion in 'Scripting' started by Kathy22, Apr 28, 2017.

  1. Kathy22

    Kathy22

    Joined:
    Jan 11, 2016
    Posts:
    1
    I am completely new to unity and I'm having some trouble. I am trying to make a motorcycle game where the bike hits a object and an information box will pop up.
    The main problem I'm having is that the bike doesn't 'stick' to the terrain. The terrain has hills and valleys, and when your press the up arrow key for the bike to move forward, it does but gradually it goes down through the terrain. I have got the gravity on and tried changing the mass of the bike to make it stay on the terrain but it doesn't. I've looked at numerous tutorials but I cant work out how to make it work, any advise and help would be amazing.


    This is my code to get the bike moving with the arrow keys.

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;



    public class Movement : MonoBehaviour {
    float Speed = 100.0f;
    float RotationSpeed = 50.0f;



    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {


    if(Input.GetKey(KeyCode.UpArrow)){
    transform.Translate(Vector3.forward * Time.deltaTime * Speed, Space.Self);
    }
    if(Input.GetKey(KeyCode.DownArrow)){
    transform.Translate(Vector3.forward * Time.deltaTime * -Speed, Space.Self);
    }
    if(Input.GetKey(KeyCode.LeftArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
    }

    if(Input.GetKey(KeyCode.RightArrow)){
    transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
    }


    }
    }
     

    Attached Files:

  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Generally speaking, you would want to set torque, force, and/or velocity on the body to move it. You can also use 'Rigidbody.movePosition' I think it's called, if you have interpolate enabled..
    Moreover, transform.translate can cause the physics interactions to be missed, I believe.
     
  3. Jofnir_IceSesh

    Jofnir_IceSesh

    Joined:
    Jan 27, 2017
    Posts:
    72
    This. I believe you are correct that physics can be missed with transform.translate. in your case, id either use torque or velocity. Just mess around with those 2 and see which is best for your model and structure