- Home /
does not want to work MoveTowards
Why does MoveTowards not want to work? Of course, if you turn on isKinematic then everything works, but this is not a way because the joints do not work well! script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;
public class ConnectObjectPlayer: MonoBehaviour
{
public Transform target;
public GameObject gmo;
public float speed;
private bool invite = false;
public HingeJoint BlazeIt;
public Rigidbody targetJoint;
void Start()
{
// BlazeIt = gameObject.GetComponent<HingeJoint>();
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
invite = true;
}
}
void OnTriggerStay(Collider other)
{
if (Input.GetKey(KeyCode.E))
{
invite = true;
transform.SetParent(other.transform, true);
gmo.transform.localRotation = Quaternion.identity;
// BlazeIt = gameObject.GetComponent<HingeJoint>();
// BlazeIt.connectedBody = targetJoint;
}
}
void OnTriggerExit(Collider other)
{
invite = false;
}
void Update()
{
float step = speed * Time.deltaTime;
if (invite)
{
if (Input.GetKeyDown(KeyCode.E))
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
// BlazeIt = gameObject.GetComponent<HingeJoint>();
//BlazeIt.connectedBody = targetJoint;
}
}
}
/* void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.R))
{
transform.SetParent(null);
}
}*/
}
Answer by WarmedxMints · Mar 18, 2019 at 11:23 AM
GetKeyDown only returns true in the frame that the key is pressed in. Therefore your code to move towards it only being executed for one frame each time the E key is first pressed. That is not the only thing wrong with your code by the sounds of it. It may be worth asking a question relating to what it is you are trying to do.
I need so that when I press a key, I approach the transport! and control with joints! Should I do a sharing with the help of a joint? just how to do it! and everything should be in OnTrigger
What you do can work, but you'll need to change:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
to
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
Input.Get$$anonymous$$eyDown, as @Warmedx$$anonymous$$ints stated, is only ran the first frame the button is pressed. Input.Get$$anonymous$$ey will be ran every frame as long as the button is held.
nothing has changed! It seems that when I arrive at this moment. it doesn't want to work
I am sorry but none of what you said has made any sense to me. What is it you are trying to do?
I have transport and equipment for him, there are joints on them, I need that when the transport runs to the equipment, when I press a key, it comes close to him and I can control it, but the problem is most likely that when I turn off the ConnectedBody using the script Owned after this, it does not want to put in its place!