У меня есть шар, который идет прямо по оси X и может двигаться влево-вправо по оси Z.Пока все хорошо, но я не хочу, чтобы мой мяч вообще шел вверх, поэтому я использую ограничения в жестком теле и блокирую Y-позицию, но это создает другую проблему, мой мяч больше не будет вращаться.Я попытался сделать это в сценарии, и происходит то же самое.Есть идеи как решить это?Я редактирую его.ЗДЕСЬ МОЙ КОД.Я надеюсь, что это поможет
private Rigidbody rb;
[Tooltip("How fast the ball moves left/right")]
public float dodgeSpeed;
[Tooltip("How fast the ball moves forwards automatically")]
[Range(0, 10)]
public float rollSpeed = 5;
//Stores the starting position of mobile touch events
private Vector2 touchStart;
void Start ()
{
// Get access to our Rigidbody component
rb = GetComponent<Rigidbody>();
}
void Update()
{
ray ();
}
void FixedUpdate ()
{
StartCoroutine (waitToStartMoving ());
}
void Movement()
{
float horizontalSpeed = 0;
/*// Check if we're moving to the side
float horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;
Vector3 movementForce = new Vector3(rollSpeed, 0.0f, horizontalSpeed);
// Time.deltaTime is the amount of time since the // last frame (approx. 1/60seconds)
movementForce *= Time.deltaTime * 60;
// Apply our auto-moving and movement forces
rb.AddForce(movementForce);
//rb.AddForce(horizontalSpeed, 0, rollSpeed);*/
// Check if Input has registered more than zero touches
if (Input.touchCount > 0)
{
// Store the first touch detected.
Touch touch = Input.touches[0];
horizontalSpeed = CalculateMovement(touch.position);
}
// Apply our auto-moving and movement forces
//rb.AddForce(horizontalSpeed, 0, rollSpeed);
var movementForce = new Vector3(rollSpeed, 0, horizontalSpeed);
// Time.deltaTime is the amount of time since the // last frame (approx. 1/60seconds)
movementForce *= (Time.deltaTime * 60);
// Apply our auto-moving and movement forces
rb.AddForce(movementForce);
}
IEnumerator waitToStartMoving()
{
yield return new WaitForSeconds (5f);
Movement ();
}
// Will figure out where to move the player horizontally
// <param name="pixelPos">The position the player has touched/clicked on</param>
// <returns>The direction to move in the x axis</returns>
float CalculateMovement(Vector3 pixelPos)
{
// Converts to a 0 to 1 scale
var worldPos = Camera.main.ScreenToViewportPoint(pixelPos);
float xMove = 0;
// If we press the right side of the screen
if (worldPos.x < 0.5f)
{
xMove = -1;
}
else
{
// Otherwise we're on the left
xMove = 1;
}
// replace horizontalSpeed with our own value
return xMove * dodgeSpeed;
}
void ray()
{
Debug.DrawRay (transform.position, Vector3.down, Color.red);
if (!Physics.Raycast(transform.position, Vector3.down, 5f))
{
rb.constraints = RigidbodyConstraints.None;
//Camera.main.GetComponent<CameraBehaviour> ().gameOver = true;
//GameObject.FindObjectOfType<Score>().stop = true;
//StartCoroutine (WaitToRestart ());
}
}
}