- Home /
AddTorque from position of mouse/touch?
Hey there,
I like to do a game, which you have an object that is jumping, depending on which place you clicked at it. Now I already have this function:
Vector3 Center = this.transform.position;
Vector3 forward = Vector3.up;
Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 forceDirection = (Center - clickedPosition).normalized;
but it doesn't look so good without rotating from the direction you clicked. That's the problem. I need help to addTorque that rotates at the opposite direction you clicked. Both left and right (z-axis). I hope everybody will understand...
Answer by K-Anator · May 06, 2019 at 11:50 PM
Sounds like you need to be working with RelativeTorque or RelativeForce. https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeTorque.html https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html
EDIT: My bad, it's actually https://docs.unity3d.com/ScriptReference/Rigidbody.AddForceAtPosition.html that's going to be helping you... I'll see if I can't whip up a script for you.
Tanks for the fast answer $$anonymous$$-ANator :D
I couldn't get this to run:
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit , 100))
{
if (hit.transform.gameObject == pusher)
{
Vector3 Center = this.transform.position;
Vector3 forward = Vector3.up;
Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 forceDirection = (Center - clickedPosition).normalized;
rb.AddForceAtPosition(forceDirection * strength, rb.transform.position,
Force$$anonymous$$ode.VelocityChange);
}
}
}
Now it's jumping good but still the rotation is zero...
Can you tell me, what is wrong?
Unfortunately I can't quite tell you what's wrong. I spent more time on this last night than I care to admit and never managed to figure out how to make a rigidbody react where I clicked it, lol. Though I did just stumble across this: https://docs.unity3d.com/ScriptReference/RaycastHit-point.html
EDIT: Actually that's a lie, I think I might know why it's not rotating. Right now you're adding force at the RB's position, which is it's anchor point. Ins$$anonymous$$d, we need to apply force where it was hit by the ray, in this case hit.point. And while were at it, there's no need to calculate the forceDirection separately since we already know the force direction, which is the direction of the ray ray.direction. What you will probably need though is a multiplier for the force, so I would suggest adding a pokeForce float as in the example code, then multiply the force direction by it.
Thank you for your help. It didnt work right with ray.direction but I replaced it with the precalculated direction and now it's working.
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.transform.gameObject == pusher)
{
Vector3 Center = this.transform.position;
Vector3 clickedPosition =
Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 forceDirection = (Center - clickedPosition).normalized;
rb.AddForceAtPosition(forceDirection * strength, hit.point);
}
}
}
Your answer
Follow this Question
Related Questions
Rigidbody Add Torque with Mouse Drag 0 Answers
Giving a ball "english" (side spin) 1 Answer
If a rigidbody is attached, should you always use AddTorque/AddRelative force for rotation/movement? 1 Answer
AddTorque stops working when model is vertical 0 Answers
Issue with Rotation behaviour of PID Controller when rotating to look at mouse position 0 Answers