Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField] private float _horizontalSpeed;
[SerializeField] private float _verticalSpeed;
private float _moveHorizontal;
private Rigidbody _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(_moveHorizontal, 0.0f, _verticalSpeed);
_rigidbody.AddForce(movement * _horizontalSpeed);
}
public void Left()
{
_moveHorizontal = -1f;
}
public void Right()
{
_moveHorizontal = 1f;
}
}
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField] private float _horizontalSpeed;
[SerializeField] private float _verticalSpeed;
private float _moveHorizontal;
private Rigidbody _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(_moveHorizontal, 0.0f, _verticalSpeed);
_rigidbody.AddForce(movement * _horizontalSpeed);
}
public void Left()
{
_moveHorizontal = -1f;
}
public void Right()
{
_moveHorizontal = 1f;
}
}
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Panel : MonoBehaviour, IBeginDragHandler, IDragHandler
{
private Ball _ball;
private void Start()
{
_ball = GameObject.FindObjectOfType<Ball>();
}
public void OnBeginDrag(PointerEventData eventData)
{
if ((Mathf.Abs(eventData.delta.x)) > (Mathf.Abs(eventData.delta.y)))
{
if (eventData.delta.x > 0)
{
_ball.Right();
}
else
{
_ball.Left();
}
}
}
public void OnDrag(PointerEventData eventData)
{
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Panel : MonoBehaviour, IBeginDragHandler, IDragHandler
{
private Ball _ball;
private void Start()
{
_ball = GameObject.FindObjectOfType<Ball>();
}
public void OnBeginDrag(PointerEventData eventData)
{
if ((Mathf.Abs(eventData.delta.x)) > (Mathf.Abs(eventData.delta.y)))
{
if (eventData.delta.x > 0)
{
_ball.Right();
}
else
{
_ball.Left();
}
}
}
public void OnDrag(PointerEventData eventData)
{
}
}