- Home /
Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player
Hello! Im currently working on my first 2D Game. There is a Knife and if you click and drag, there is a Line. The knife flies in the direction of the line and the longer the line is, the faster the knife flies. Now here is my Problem. You can hold anywhere on the screen to drag the Line. And while Holding the Line, the Knife should turn in the direction to where it will fly. But if i just make the knife follow my mouse Position but the line is not in the center of the screen, then the knife points to a wrong direction. So then I thought it would be possible to take the Rotations/Positions of the Knife and Equal it to the Positions of the Line Renderer. But how can i do that? Any suggestions? if it helps: Here are some of my Scripts that i use.
Here is the Movement Speed of my Player (you drag the Line, then the player gets an impulse to the opposite direction):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Physics2DExtensions
{
public static void AddForce(this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode = ForceMode.Force)
{
switch (mode)
{
case ForceMode.Force:
rigidbody2D.AddForce(force);
break;
case ForceMode.Impulse:
rigidbody2D.AddForce(force / Time.fixedDeltaTime);
break;
case ForceMode.Acceleration:
rigidbody2D.AddForce(force * rigidbody2D.mass);
break;
case ForceMode.VelocityChange:
rigidbody2D.AddForce(force * rigidbody2D.mass / Time.fixedDeltaTime);
break;
}
}
public static void AddForce(this Rigidbody2D rigidbody2D, float x, float y, ForceMode mode = ForceMode.Force)
{
rigidbody2D.AddForce(new Vector2(x, y), mode);
}
}
public class DragNShoot : MonoBehaviour
{
public float power = 10f;
public Rigidbody2D rb;
public Vector2 minPower;
public Vector2 maxPower;
DragLine tl;
Camera cam;
Vector2 force;
Vector3 startPoint;
Vector3 endPoint;
private void Start()
{
cam = Camera.main;
tl = GetComponent<DragLine>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
startPoint.z = 15;
}
if (Input.GetMouseButton(0))
{
Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
currentPoint.z = 15;
tl.RenderLine(startPoint, currentPoint);
}
if (Input.GetMouseButtonUp(0))
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
endPoint.z = 15;
force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
rb.AddForce(force * power, ForceMode.Impulse);
}
}
}
Second Script is the Scipt which rotates the Player towards the velocity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateTowardsVelocity : MonoBehaviour
{
public float rotateSpeed = 900;
public Vector2 rotation;
private void Update()
{
var dir = GetComponent<Rigidbody2D>().velocity;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
var q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotateSpeed * Time.deltaTime);
if (Input.GetMouseButtonDown(0))
{
rotateSpeed = 0;
}
if (Input.GetMouseButtonUp(0))
{
rotateSpeed = 900;
}
}
}
The 3rd script is the Script which dont work right. Right now the Knife follows the Mouse while holding down the mouse button but the rotation is not equal to the direction of the line and the direction of the moving player. I hope you understand what i mean.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aimwhileholding : MonoBehaviour
{
public float speed = 5f;
private void Update()
{
if (Input.GetMouseButton(0))
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.smoothDeltaTime);
}
}
}
The last Script is the one of my Drag Line, so the line from which i want to set the position equal.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
public class DragLine : MonoBehaviour
{
public LineRenderer lr;
private void Awake()
{
lr = GetComponent<LineRenderer>();
}
public void RenderLine(Vector3 startPoint, Vector3 endPoint)
{
lr.positionCount = 2;
Vector3[] points = new Vector3[2];
points[0] = startPoint;
points[1] = endPoint;
lr.SetPositions(points);
}
private void Update()
{
if (Input.GetMouseButtonUp(0))
{
lr.positionCount = 0;
}
}
}
I hope that someone can help me! Tim
Your answer
Follow this Question
Related Questions
Unity prediction line renderer 0 Answers
How to rotate parent only without affecting children? 3 Answers
Rotation of player based on camera direction and joystick direction in 3D world 1 Answer
Line Renderer Rotation Issue 2 Answers
Object not rotating when the associated trigger is triggered 1 Answer