Question by
amirmohammadk · Jan 17, 2019 at 05:11 PM ·
2d-physicsspringjoint
max stretch for spring joint2d
Hello, I'm making a slingshot using a spring joint 2d . and I want to set a max stretch for it by code.Thank you for helping. Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public float maxDistance1 = 150;
private SpringJoint2D rockjoint;
public Transform rocktrans;
public Transform rp;
public Rigidbody2D rb2;
public bool touching = false;
public bool dragging = false;
public bool finisheddrag = false;
void Start()
{
rockjoint = GetComponent<SpringJoint2D>();
}
void Update ()
{
if (dragging == true){
if(finisheddrag == true){
}else if (finisheddrag == false){
if (touching == true)
{
}
else if(touching == false)
{
Vector3 temp = rocktrans.position;
temp = Input.mousePosition; // modify the component you want in the variable...
temp.y = Input.mousePosition.y;//-60.3f; // modify the component you want in the variable...
temp.x = Input.mousePosition.x;
rocktrans.position = temp;
}
}
}else if(dragging == false){
rocktrans.position = rocktrans.position;
}
}
void OnMouseDrag()
{
dragging = true;
// and save the modified value
}
void OnMouseUp()
{
finisheddrag = true;
dragging = false;
// If your mouse hovers over the GameObject with the script attached, output this message
rb2.isKinematic = false;
StartCoroutine(Example());
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "rp")
{
rockjoint.enabled = false;
}
if (other.tag == "a")
{
touching = true;
}
}
IEnumerator Example()
{
yield return new WaitForSeconds(0.4f);
rockjoint.enabled = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Unrealistic 2D Bouncing 2 Answers
Move an Object by clicking on it 1 Answer
2D idle game character movement/animations 0 Answers
Raycast doesn't find mesh 0 Answers