Пример кода брал тут - http://www.flashgametuts.com/tutorials/ ... s3-part-2/
У меня получился такой код:
Синтаксис:
Используется csharp
void Update ()
{
if (XSpeed >= MaxXSpeed)
{
XSpeed = MaxXSpeed;
}
if (ZSpeed >= MaxZSpeed)
{
ZSpeed = MaxZSpeed;
}
if (isStart)
{
rigidbody.AddForce(Sign * new Vector3(XSpeed, 1, ZSpeed));
}
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "Brick")
{
isStart = false;
float ballPosition = gameObject.transform.position.x - theCollision.gameObject.transform.position.x;
Debug.Log("ballPosition" + ballPosition);
//hitPercent converts ballPosition into a percent
//All the way to the left is -.5
//All the way to the right is .5
//The center is 0
float hitPercent = (ballPosition / (theCollision.gameObject.transform.localScale.x - gameObject.transform.localScale.x)) - 0.5f;
Debug.Log("Hit percent" + hitPercent);
//Gets the hitPercent and makes it a larger number so the
//ball actually bounces
XSpeed = hitPercent * Coefficient;
Debug.Log("XSpeed" + XSpeed);
//Making the ball bounce back up
Debug.Log("ZSpeed" + ZSpeed);
ZSpeed *= -1;
Debug.Log("ZSpeed" + ZSpeed);
rigidbody.AddForce(new Vector3(XSpeed, 1, ZSpeed));
}
}
{
if (XSpeed >= MaxXSpeed)
{
XSpeed = MaxXSpeed;
}
if (ZSpeed >= MaxZSpeed)
{
ZSpeed = MaxZSpeed;
}
if (isStart)
{
rigidbody.AddForce(Sign * new Vector3(XSpeed, 1, ZSpeed));
}
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "Brick")
{
isStart = false;
float ballPosition = gameObject.transform.position.x - theCollision.gameObject.transform.position.x;
Debug.Log("ballPosition" + ballPosition);
//hitPercent converts ballPosition into a percent
//All the way to the left is -.5
//All the way to the right is .5
//The center is 0
float hitPercent = (ballPosition / (theCollision.gameObject.transform.localScale.x - gameObject.transform.localScale.x)) - 0.5f;
Debug.Log("Hit percent" + hitPercent);
//Gets the hitPercent and makes it a larger number so the
//ball actually bounces
XSpeed = hitPercent * Coefficient;
Debug.Log("XSpeed" + XSpeed);
//Making the ball bounce back up
Debug.Log("ZSpeed" + ZSpeed);
ZSpeed *= -1;
Debug.Log("ZSpeed" + ZSpeed);
rigidbody.AddForce(new Vector3(XSpeed, 1, ZSpeed));
}
}
Вроде всё считается, но мяч не отскакивает((
В чём проблема? Или , возможно, у кого то есть более лучший алгоритм отскакивания мяча. Мне нужно , чтобы всё было максимально реалистично.
Заранее спасибо. Сильно прошу не пинать, изучаю только неделю)))
http://rapidshare.com/files/419666090/a ... itypackage