Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
float speedRotation = 2f;
float speed = 5f;
int direction = 0;
void Start()
{
}
void Update()
{
gameObject.transform.Rotate(Vector3.forward * speedRotation * direction);
if (speed > 0)
{
gameObject.transform.Translate(0f, speed * Time.deltaTime, 0f);
speed -= 0.05f;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 inDirection = gameObject.transform.position;
Vector2 inNormal = collision.contacts[0].normal;
Vector2 newVelocity = Vector2.Reflect(inDirection, inNormal);
gameObject.GetComponent<PolygonCollider2D>().enabled = false;
float rot = 90 - Mathf.Atan2(newVelocity.x, newVelocity.y) * Mathf.Rad2Deg;
gameObject.transform.eulerAngles = new Vector3(0, 0, rot);
print("inDirection: " + inDirection + ", inNormal: " + inNormal + ", newVelocity: " + newVelocity);
}
public void Rotate(int direction)
{
this.direction = direction;
}
}
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
float speedRotation = 2f;
float speed = 5f;
int direction = 0;
void Start()
{
}
void Update()
{
gameObject.transform.Rotate(Vector3.forward * speedRotation * direction);
if (speed > 0)
{
gameObject.transform.Translate(0f, speed * Time.deltaTime, 0f);
speed -= 0.05f;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
Vector2 inDirection = gameObject.transform.position;
Vector2 inNormal = collision.contacts[0].normal;
Vector2 newVelocity = Vector2.Reflect(inDirection, inNormal);
gameObject.GetComponent<PolygonCollider2D>().enabled = false;
float rot = 90 - Mathf.Atan2(newVelocity.x, newVelocity.y) * Mathf.Rad2Deg;
gameObject.transform.eulerAngles = new Vector3(0, 0, rot);
print("inDirection: " + inDirection + ", inNormal: " + inNormal + ", newVelocity: " + newVelocity);
}
public void Rotate(int direction)
{
this.direction = direction;
}
}
К примеру если треугольник летит к плоскости под углом 30 градусов, то отраженный, в коде rot, = -84, при следующих значения векторов inDirection: (0.9, 9.1), inNormal: (0.0, -1.0), newVelocity: (0.9, -9.1)