- Home /
hit.parent doesn't work when applied to physics GameObjects using Constant force
Hello guys! I am having a problem with some code.
I have a build script which attaches cubes (ship components) to other cubes (ship components) and to this day it is working PERFECTLY!
Only issue I am having now is that when i attach a constant force to this object, and try to child one of the other cubes to it, the cube refuses to child itself to that parent cube.
Before i added constant force to the parent cube, it worked fine and each cube child'd itself just fine. They would all move with the parent and so on.
Is there anything i am missing here or something i have forgotten to do?
Script:
var blockLayer : LayerMask = 1;
var range : float = 100;
var hit : RaycastHit;
var blocks : GameObject[];
var chosenB : GameObject;
static var shipStart : boolean;
var shipBegin : GameObject;
var shipParent : Transform;
var shipSpawn : Transform;
var player : Transform;
var instantiatedObject;
var isC = false;
////////////////////////////////////////////////////
var snapDirections : Vector3[];
var closestSnapDirection : Vector3;
var forwardVar : Vector3;
////////////////////////////////////////////////////
function Update () {
snapDirections[0] = Vector3.right;
snapDirections[1] = Vector3.left;
snapDirections[2] = Vector3.forward;
snapDirections[3] = Vector3.back;
//snapDirections[4] = Vector3.up;
//snapDirections[5] = Vector3.down;
if(Input.GetKeyDown(KeyCode.Alpha0))
{
chosenB = null;
}
if(Input.GetKeyDown(KeyCode.Alpha1))
{
chosenB = blocks[0];
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
chosenB = blocks[1];
}
if(Input.GetKeyDown(KeyCode.Alpha3))
{
chosenB = blocks[2];
}
if(Input.GetKeyDown(KeyCode.Alpha4))
{
chosenB = blocks[3];
}
if(Input.GetKeyDown(KeyCode.Alpha5))
{
chosenB = blocks[4];
}
if(Input.GetKeyDown(KeyCode.Alpha6))
{
chosenB = blocks[5];
}
if(Input.GetKeyDown(KeyCode.Alpha6))
{
chosenB = blocks[6];
}
if(Input.GetKeyDown(KeyCode.Alpha7))
{
chosenB = blocks[7];
}
if(Input.GetKeyDown(KeyCode.Alpha8))
{
chosenB = blocks[8];
}
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
if(Input.GetKeyDown(KeyCode.B))
{
shipStart = !shipStart;
isC = true;
}
if (Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer))
{
chosenB.transform.position = hit.normal;
}
}
function Build() {
if (HitBlock() && hit.transform.tag == "Block" || HitBlock() && hit.transform.tag == "Helm" || HitBlock() && hit.transform.tag == "Weapons")
{
var dotProductClosestToOne = Mathf.NegativeInfinity;
var playerForward = player.transform.forward;
for(i = 0; i < snapDirections.Length; i++) ///////////////////////////
{
var currDotProduct : float = Vector3.Dot(snapDirections[i], player.transform.forward);
if(currDotProduct>dotProductClosestToOne)
{
dotProductClosestToOne = currDotProduct;
closestSnapDirection = snapDirections[i];
}
}/////////////////////////////////////////////////////////////////////
var interF = GameObject.Instantiate(chosenB);
interF.transform.rotation = Quaternion.LookRotation(closestSnapDirection, Vector3.up);
interF.transform.position = hit.transform.position + hit.normal;
interF.transform.parent = hit.transform.parent;
}
}
function Erase() {
if (HitBlock() && hit.transform.tag == "Block" || HitBlock() && hit.transform.tag == "Helm" || HitBlock() && hit.transform.tag == "Weapons")
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
function OnGUI()
{
if(shipStart == true)
{
if(GUI.Button(Rect(1,1,100,20), "Create Ship"))
{
var ship = Instantiate(shipBegin, shipSpawn.transform.position, Quaternion.identity);
shipP = Instantiate(shipParent, shipSpawn.transform.position, Quaternion.identity);
ship.transform.tag = "Block";
ship.transform.parent = shipP.transform;
}
}
}
Any hep would be greatly appreciated! Thank you once again for reading, Sincerely,
bakos
As an update i seemed to have found a problem within my code, but it does not fix my issue.
if i change:
interF.transform.parent = interF.collider.transform.parent;
It does the same thing. It leaves the children outside of the parent. Although if i change it to:
interF.transform.parent = interF.collider.transform;
It applies the child to the parent, but deleting any one of these children would delete the entire creation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add wind effect to Game Object 1 Answer
what is the comand for global torque using constant force 1 Answer
ConstantForce on RigidBody2D 0 Answers
Constant Running on x axis 0 Answers
keep my collider on my particle system close together 1 Answer