- Home /
Question by
Mikut · Apr 26, 2020 at 06:37 PM ·
2dlinerendererline renderertop-down
LineRenderer acts strangely
Hi! I've made a cube that rotates in direction of mouse and renders line from its position upwards (2D). The problem is that when cube is moving, line renders really deviated from cube's transform.up. It happens also when cube is just moved a little bit from its spawn point and then line is rendered.
Here's my shooting script:
public class Shoot : MonoBehaviour
{
[SerializeField]
Transform t;
Ray2D r;
RaycastHit2D rh;
LineRenderer lr;
Vector2 rayDirection;
// Update is called once per frame
void Update()
{
r = new Ray2D(t.position, t.up * 5f);
Debug.DrawRay(t.position, t.up * 5f);
rayDirection = r.direction;
if (Input.GetMouseButtonDown(0))
{
lr = gameObject.AddComponent<LineRenderer>();
lr.positionCount = 2;
lr.useWorldSpace = true;
lr.enabled = true;
}
if (Input.GetMouseButton(0))
{
lr.SetPosition(0, r.origin);
lr.SetPosition(1, rayDirection * 5f);
}
if (Input.GetMouseButtonUp(0))
{
lr.enabled = false;
Destroy(lr);
}
}
}
And here's my movement script:
public class Movement : MonoBehaviour
{
[SerializeField]
Rigidbody2D rb;
Vector2 movement;
float movespeed = 5f;
Vector2 mousePos;
void Start()
{
}
// Update is called once per frame
void Update()
{
movement.y = Input.GetAxisRaw("Vertical");
movement.x = Input.GetAxisRaw("Horizontal");
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate()
{
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
rb.MovePosition(rb.position + movement * movespeed * Time.fixedDeltaTime);
}
}
There's a screenshot attached below that should describe my problem a little bit better.
What can possibly cause this issue? What did I do wrong?
zrzut-ekranu-165.png
(145.2 kB)
Comment