- Home /
2x GetComponent ConstantForce at the same time?
I need to use both
GetComponent<ConstantForce> ().force = new Vector3 (Input.GetAxis ("Mouse X") * speed , 0f, Input.GetAxis ("Mouse Y") * speed);
GetComponent<ConstantForce> ().force = Vector3.MoveTowards( transform.position*2, transform.position*2, Time.deltaTime*2);
At the same time. Is it possible? I need player to both control object with ConstantForce and to have MoveTowards away from zero - at the same time... Currently the last ConstantForce always works, while the first not
Answer by Eno-Khaon · Jun 14, 2015 at 05:29 AM
Vector3 values can be added together.
Vector3 first = new Vector3 (Input.GetAxis ("Mouse X") * speed , 0f, Input.GetAxis ("Mouse Y") * speed);
Vector3 second = Vector3.MoveTowards( transform.position*2, transform.position*2, Time.deltaTime*2);
GetComponent<ConstantForce> ().force = first + second;
Edit: That said, you could probably stand to use a much simpler approach, anyway.
GetComponent<ConstantForce>().force = Vector3.MoveTowards( transform.position*2, transform.position*2, Time.deltaTime*2);
GetComponent<Rigidbody>().AddForce(new Vector3 (Input.GetAxis ("Mouse X") * speed , 0f, Input.GetAxis ("Mouse Y") * speed));
Add Force isn't working as i need :) Where to put GetComponent in first example?
Well, i did
GetComponent ().force = new Vector3 (Input.GetAxis ("$$anonymous$$ouse X") speed, 0f, Input.GetAxis ("$$anonymous$$ouse Y") speed) + Vector3.$$anonymous$$oveTowards( transform.position*towardSpeed, transform.position*towardSpeed, Time.deltaTime*towardTime);
And it worked. Thnx :)
I am going to close Question and choose your answer as best, but for a second - maybe do you know how to make IF statement when object moves ONLY to Vector3.zero???
Hmm... To be honest, your question here's a little unclear. If you mean an if statement when the only force applied is from the ConstantForce, you could use:
if(Input.GetAxis("$$anonymous$$ouse X") == 0.0f && Input.GetAxis("$$anonymous$$ouse Y") == 0.0f)
{
// Do something
}
On the other hand, if you mean an if statement when the only direction of movement is the ConstantForce, you could use:
if(Vector3.Dot(GetComponent<Rigidbody>().velocity.normalized, GetComponent<ConstantForce>().force.normalized) == 1.0f)
{
// $$anonymous$$oving exactly away from Vector3.zero
}
if(Vector3.Dot(GetComponent<Rigidbody>().velocity.normalized, GetComponent<ConstantForce>().force.normalized) == -1.0f)
{
// $$anonymous$$oving exactly toward Vector3.zero
}
Also, I realized the typo I made on the "AddForce" answer I gave. I added an extra capital letter to "Rigidbody" by accident. Corrected that now.
Your answer
Follow this Question
Related Questions
Click Two objects At Once Windows 7 Touch 1 Answer
Remove space between two objects at runtime 0 Answers
ConstantForce not producing movement 4 Answers
Rigidbody slowly moves 1 Answer