Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dvxl · Mar 27, 2020 at 11:29 AM · rotation2d gameplayer movementlinerenderervector

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

194 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges