Hi there all, I'm currently struggling to AddForce to an object, the object is a Domino and what i am attempting to achieve, that when i click on either 2 of the main faces (back and front) the domino will fall in the corresponding click direction, i do not however wish for the user to be able to 'tip' the domino sideways, only backward and forward, in relation to whatever the dominos rotation may be.
This piece of code is what I started with, but it would only push the domino in one specified 'world' direction, rotation of the domino made no difference to the direction of force it would always push 'North'.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.CompareTag("Domino"))
{
hitInfo.collider.GetComponent().AddForce(transform.forward * forceAmount);
}
}
This piece of code is my current piece where I am struggling, the result with this code is that the domino 'hops' toward the camera when clicked and not at the direction that the mouse clicked.
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var mouseDir = mousePos - gameObject.transform.position;
mouseDir.z = 0.0f;
mouseDir = mouseDir.normalized;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.CompareTag("Domino"))
{
hitInfo.collider.GetComponent().AddForce(mouseDir * forceAmount);
}
}
}
Below are some GIFs, visualizing what is happening in-game.
Old Code:
![Old Code][1]
New Code:
![New Code][2]
[1]: https://im5.ezgif.com/tmp/ezgif-5-84c69cd86d10.gif
[2]: https://im5.ezgif.com/tmp/ezgif-5-7300439bc6af.gif
Main Point:
How can i make it so if i click the face of the domino, it will fall onto the back side and clicking the back makes it fall to its face, no matter the rotation of the domino.
↧