- Home /
Can you use a Distance Joint without the "pulling"?
Hey people! This is my first time posting so I hope this is the right place to place my question =)
So this is a fairly complicated problem: I`m working on a 2D project with 4 characters which are controlled by 4 different players and 1 vehicle. All of the characters can get into the vehicle and move it around. What I'm trying to do is have my 4 characters at all times be within a 20 units radius from the vehicle. Once a character reaches the 20 units limit both the character and the vehicle should not be able to move anymore unless the movement brings them closer together again.
I have tried using a distance joint on each character connecting them to the vehicle, but if the vehicle moves further away than the distance joint allows it it just "pulls" the characters along as it is much heavier than the characters.
My actual "solution" is a distance joint - so the character wont move further away than 20 units - paired with this code on the vehicle:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class vehicleController : MonoBehaviour
{
private GameObject player1;
private float dist1;
private float maxDis = 19.5f;
private slotSelect slotScript;
// Use this for initialization
void Start()
{
slotScript = GetComponent<slotSelect>();
player1 = GameObject.FindWithTag("P1");
}
// Update is called once per frame
void Update()
{
// Calculate distance to player
dist1 = Vector3.Distance(player1.transform.position, transform.position);
if (dist1 >= maxDis)
{
canMove = false;
}
else
{
canMove = true;
}
if (slotScript.slotMove1 == true && canMove == true)
{
//MOVING CODE
}
Basically, I just freeze the vehicle's position until the character gets closer again, but I would like the vehicle to be able to move closer to the character as well.
Any help is appreciated! =)
Your answer
Follow this Question
Related Questions
how do I make a joint that fixates the objects distance but lets them rotate freely? 1 Answer
Hinge joint and fixed distance 0 Answers
Two way joint? 1 Answer
Joint Break Force Distance 0 Answers
keeping constant "Distance Joint 2d" 2 Answers