- Home /
Swing between two points
I have two positions. One is a stationary point. I have a line renderer that I draws a line between the two of them. I wan't the second point to swing from the first point like its the end of a rope, without adding any hingeJoints. How would I be able to do this?
I'm making a grappling hook that sucks in the second point closer towards the first, and I want it to swing realistically.
Answer by andrew-lukasik · Jun 28, 2021 at 07:50 AM
Pendulum might offer some help here as it roughly approximates this motion.
using UnityEngine;
public class SimplePendulum : MonoBehaviour
{
[SerializeField][Range(0,90)] float _swingAngle = 33;
[SerializeField][Min(0.001f)] float _length = 1.4f;
[SerializeField][Min(0.001f)] float _gravity = 6f;
[SerializeField] float _azimuth = 0;
void Update ()
{
float time = Time.time;
float angle = -90f + ( _swingAngle*Mathf.Deg2Rad * Mathf.Cos( Mathf.Sqrt(_gravity/_length) * time ) ) * Mathf.Rad2Deg;
// based on: https://gamedev.net/forums/topic/624653-how-to-code-a-swinging-movement/4938528/
Vector3 p0 = transform.position;
Vector3 p1 = p0 + Quaternion.Euler( 0 , _azimuth , angle ) * new Vector3{ x=_length };
Debug.DrawLine( p0 , p1 , Color.yellow );
}
}
Your answer
Follow this Question
Related Questions
Need Help Getting Rope To Wrap/Straighten In 3D Space 0 Answers
Gold Miner Hook? 1 Answer
Pulling objects towards eachother? 1 Answer
Change Script so that the object's position can be modified in the Editor/through other scripts? 1 Answer
Creating a dynamic fishing line with line rendering, but the last segment is stiff? 0 Answers