8
8
8_JOHN_82018-12-01 22:33:56
Unity
8_JOHN_8, 2018-12-01 22:33:56

How to solve the problem with the gradual acceleration of the object and its jump?

/*
The object is - a sphere!!!
The essence of the problem is that when an object accelerates through Acceleration, the sphere after a while begins to rise upwards. When you try to jump without using the reverse momentum, the sphere is in the air longer than necessary, and when you set the reverse momentum, the whole jump looks terribly unnatural. Please help which is better to use?
*/

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

public class BallController : MonoBehaviour {

    public float speed;
    public float fast;
    float LR;
    Vector3 coordinats;
    public float magnitude;
    public Rigidbody RB;
    public float jump;
    public bool ground;
    public float speed_ball;

    private void FixedUpdate()
    {
        Ray ray = new Ray(RB.transform.position, Vector3.down);
        RaycastHit rh;
        if (Physics.Raycast(ray, out rh, 0.8f))
        {
            ground = true;
        }
        else
        {
            ground = false;
        }
        if ((Input.touchCount > 0) && (ground == true))
        {
            //RB.AddForce(Vector3.zero);
            RB.AddForce(Vector3.up * jump, ForceMode.Impulse); // прыжок вверх
            //RB.useGravity = true;
        }
        if ((ground == false) && RB.transform.position.y > 1f)
        {
            //RB.AddForce(Vector3.zero);
            RB.AddForce(-Vector3.up * jump, ForceMode.Impulse); // даю объекту обратный импульс
        }

        /////////

        GetComponent<Rigidbody>().AddForce(Vector3.forward * fast, ForceMode.Acceleration);
        coordinats = Input.acceleration; // сохраняет данные акселерометра в переменную
        if (coordinats.sqrMagnitude > magnitude)
        {
            coordinats.Normalize();
        }
            
        //coordinats.Normalize();

        if (coordinats.x > 0.1f)
        {LR = 0.15f; transform.Translate(new Vector3(LR * speed * Time.deltaTime, 0, 0));} // поворачивает вправо
        if (coordinats.x < -0.1f)
        { LR = -0.15f; transform.Translate(new Vector3(LR * speed * Time.deltaTime, 0, 0)); } // поворачивает влево
    }

    private void Start()
    {
        RB = GetComponent<Rigidbody>();
        RB.AddForce(Vector3.forward * 10f, ForceMode.Impulse); // дает толчок шарику при старте
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question