Search Unity

Object not landing flat with gravity

Discussion in 'Physics' started by cjlynch38, Apr 24, 2017.

  1. cjlynch38

    cjlynch38

    Joined:
    Apr 24, 2017
    Posts:
    1
    Hello ~

    I have an object with a basic "jump" script that also rotates the object (kind of a backflip sort of motion). However whenever it lands on the ground at a certain angle (like shown in the picture), it does not level back to a flat position, but rather just stands up sort of unnaturally. I am trying to configure it so that what ever position it lands, it flattens out to the nearest edge, like how it would in real-world physics. Here is my script:



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed = 5f;
    8.     public float jumpSpeed = 10f;
    9.     private float movement = 0f;
    10.     private Rigidbody rigidBody;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         rigidBody = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.  
    22.         movement = Input.GetAxis("Horizontal");
    23.         if (movement > 0f)
    24.         {
    25.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    26.         }
    27.         else if (movement < 0f)
    28.         {
    29.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    30.         }
    31.         else
    32.         {
    33.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    34.         }
    35.         if (Input.GetButtonDown("Jump"))
    36.         {
    37.          
    38.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    39.             rigidBody.AddTorque(transform.forward * 50);
    40.  
    41.         }
    42.     }
    43. }