I don't know how to have my weapon follow where the camera moves

I’ve been looking around all day for an answer, and there’s no good ones. I need to know how to have my weapon follow the center of the camera and stay attached to my character, and nothing is working. Help is appreciated.

1 Answer

1

First I will explain this to you if you cant figure out the code ill provide it to you later (wont give now as im low on time, came here to put a question of my own before studying) First shoot a raycast from ur camera center, see where it hits, store this as hitPoint (if it does not hit anything do - Ray.GetPoint(75f)), now subtract the weapon pos from hitPoint to get the direction and then rotate the weapon respectively, now it will point to where the crosshair is, now just attach it to ur character using IK, again, I typed this in a hurry, if you still cant figure it out ill be happy to give the code :slight_smile: , ill also reccomend you a good, basic ik asset if you dont already use one

    private void shootingCalculations()
    {
        Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Ray from the center of the camera/viewport
        RaycastHit hit; //To store where it hit
        if (Physics.Raycast(ray, out hit) && hit.collider.gameObject.layer != 3 && hit.collider.gameObject != gameObject)
        {
            targetPoint = hit.point; //If gameobject is not layer 3(player layer) or is not gameobject and the ray actually hit something,  then the target point is where the ray hit, I had some messy colliders so I did all this, but just 'Physics.Raycast(ray, out hit)' is enough
        }
        else 
        {
            targetPoint = ray.GetPoint(75); //If the ray does not hit anything, then we take a really far point on the ray as out target
        }

        Qyaternion target = Quaternion.LookRotation(targetPoint - transform.position); //Calculating the target
        gunHolder.transform.rotation = target; //Setting the rotation, I had an empty parent for all my weapons called gunHolder which was placed at the shoulder for more realistic rotations, directly rotating the gun will work too tho 
    }

In this code, I use the target point for both calculating the rotation and firing the bullet in the desired direction, also I learnt a lot of this logic from this video-
This

Okay, take your time if needed. This is a lot of help, and I thank you for that. I would appreciate some example code if you could show some.

Okay, just got the gun to stay with the character, but the gun is still not looking at the cross hair when I look up or down, only side to side. Any help on that?

Yep, forgot to check after studying yesterday lol, just got up, anyway, heres a code, I added comments- (I called this function in void update, but see what works best for you) I edited the main answer and added the code

alright, gave it a shot. It's returning some errors saying that cam, targetPoint, and gunHolder don't exist in the context, so I'm guessing that's where I step in and do the rest. Thank you for your help!

yep, read the comments, they tall you exactly what everything is