- Home /
How best to implement attaching an object to a transport?
when the transport runs to the trigger object, when you press a key, the object will be assigned to parental Here is a photo ![alt text][1]
It would be desirable that when you press a key, the project hit is attached in this way ![alt text][2]
The question is how to implement this idea! With the help of animation,? [1]: /storage/temp/130569-tracto1.png [2]: /storage/temp/130570-tracto3.png
Answer by JxWolfe · Jan 05, 2019 at 07:16 PM
bool canAttach;
GameObject attachable;
public Vector3 attachedPosition;
void Update()
{
if(Input.GetKey(KeyCode.Space))
{
if(canAttach)
{
attachable.transform.parent = gameObject.transform;
attachable.transform.localPosition = attachedPosition;
}
}
}
OnTriggerEnter(Collider other)
{
if(other.tag == "attachable")
{
canAttach=true;
attachable = other.gameObject;
}
}
OnTriggerExit(Collider other)
{
if(other.tag == "attachable")
{
canAttach=false;
attachable = null;
}
}
this should work now
knocks this in the console Assets/$$anonymous$$ainSkripts/TriggerTest.cs(19,63): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
remove the
attachable = null;
guess it didn't like that
no, it's all the same problem
Assets/$$anonymous$$ainSkripts/TriggerTest.cs(19,54): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to
UnityEngine.Transform'
shoot... I'm idiotic. Sorry for that...
attachable.transform.parent = gameObject.transform;
that'll fix the problem. sorry again. :(
Thanks for the answer. The error was corrected, but another one appeared! when you run to the object and press save, the transport jerks and flies under the picture
Your answer
Follow this Question
Related Questions
Attachment of equipment for transport 0 Answers
Attachment of equipment for transport 0 Answers
Animation Starts on begin play 2 Answers