I have some enemies in my VR game that are heavily armored, so the players swords can’t slice them. I want to make it more clear that the swords aren’t sharp or strong enough so I want to have the sword bounce off of the armor when the player tries to cut it, with the bounciness being determined by how fast the player’s hand is moving.
I have two questions:
I want to use Rigidbody.AddForce() to do this, with the rigid body being on the hand mesh. But I don’t want the hand to be affected by the rigid body in any way other than the addforce method because otherwise it would move out of place. How can I get this to work? Should I just avoid rigidbodies altogether and make my own custom AddForce script?
How would I override the controller tracking in whatever direction the hand is bouncing but no other directions? I can’t just disable the tracking on the controller altogether, that would feel janky. What should I do for this?
Isn’t the player’s actual hand input driving the position at all times??
If you wanted to make the sword recoil while the user’s hand (obviously) continued through the swash, one way would be to hide the actual sword the player is swinging and make a new fake one at the same spot, then recoil it the other way, sending it flying.
But either way, parenting its pivot (handle?) to the hand would still look weird because that would be on the other side of the enemy already, having cut through air.
EDIT: another way might be to make the hand-driven sword disappear, and show a huge flash of sword fragments, and the player has no sword now, just a handle. They have to wiggle their hand to shake a new sword back into place, and all the pieces would rejoin and they’d be back in play?
Yeah this is my bad, I should’ve been more clear about this but the sword is attached the player’s arm. The player has cyborg arms and the swords come out of them like wolverine claws. I should’ve said I’m trying to add recoil to the whole mesh, not just the sword. It might be better to only stop the hand from going through the enemy and not bother bouncing it.
I’m trying to use a character controller to do it now, but this code isn’t working.
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class AntiHandNoClip : MonoBehaviour
{
public CharacterController hand;
private XRController controller;
private Vector3 lastPos;
private void Start()
{
controller = transform.parent.parent.GetComponent<XRController>(); // get reference to the character
transform.parent = null; // unparent from the controller
lastPos = controller.transform.position; // get position in the first frame
}
private void LateUpdate()
{
Vector3 posDifference = lastPos - controller.transform.position; // get amount controller moved
hand.Move(transform.position + posDifference); // move to our current position + amount controller moved
lastPos = controller.transform.position; // reset lastPos
}
}
I’m trying to get the amount that the controller moved from the last frame to the next and then have the character controller handle add that to our current position and have the character controller handle the not going through walls. But all this does is make the controller continuously move backwards.
I’m confused, I thought CharacterController.Move() would teleport to a position, but it seems to be adding to the hands position.
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class AntiHandNoClip : MonoBehaviour
{
public CharacterController hand;
private XRController controller;
private void Start()
{
controller = transform.parent.parent.GetComponent<XRController>();
transform.parent = null; // unparent from the control
}
private void LateUpdate()
{
hand.Move(controller.transform.position);
}
}
I think Kurt’s earlier point, and I will re-iterate it, is that in VR you cannot stop the player’s hands from doing anything (unless they’re standing near some furniture or family members). If you try to stop the Virtual Hands but the player’s Real Hands continue to sweep through the air, this breaks the illusion that you’re trying to maintain in VR. Kurt’s idea to shatter the sword and let you re-build it was to allow you to maintain the motion illusion that is very important in VR. To repeat another way: the Virtual Hands really must follow the Real Hands no matter what, and you need to explain what happens in your virtual world in a way that keeps that relationship.
I do see his point, but I could always have a setting for ghost hands. I don’t think I’m gonna go with the idea I had in mind originally any way though. Mostly because it just feels kind of janky and it doesn’t match the game. I’m gonna think of something else besides the swords shattering since it goes against mechanics I already have.