Reputation: 21
Alright so im working with swinging effect in unity FPS character controller where i need to add a swinging part . Im trying to have a swing smooth direction based on the hit point using raycasting and transform.position . For My heirarchy i have a Player who has a Main Camera as child and i have gun whose a child of main camera and a parent of a guntip where the rope will struck off from . Im trying to rotate using Cross product of the hit point and the player transform position but i cant get it right and i cant find any other way to do it as well .
void start Swinging()
{
timer += Time.deltaTime * 12f ;
Vector3 swingDirection = Vector3.Cross(transform.localPosition, (hook.transform.position - transform.position).normalized).normalized;
float speed = Mathf.PingPong(timer, 180);
velocity.y = 0;
transform.Translate(swingDirection * speed * Time.deltaTime);
}
void createHook()
{
RaycastHit hit;
Ray ray = new Ray(camerapos.position, camerapos.forward);
if (Physics.Raycast(ray, out hit, 100f, hookMask))
{
hook = Instantiate(hookprefab, hit.point, Quaternion.identity);
hookNormal = hit.normal;
canSwing = true;
}
}```
**Here's the code I am working currently on where I am calling them when the mouse is clicked once for the Create Hook and when the mouse is held down the swinging function**
Would be glad if you all can share your thoughts on how to do it right way I'm a beginner in unity currently learning.
Upvotes: 1
Views: 221