- Home /
Question by
xNIkurasu · May 24, 2018 at 05:54 AM ·
c#rotationweaponthird person shooter
LookAt + Raycasting makes weapon rotate in a weird way
Hello! So, I'm currently trying to make a third person shooter and now im at the point where im trying to get my weapon to shoot exactly towards the middle of my screen, and for that I tried using Raycast and LookAt methods:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ddddd : MonoBehaviour {
public Camera cam;
// Update is called once per frame
void Update () {
}
void Aim()
{
float screenX = Screen.width / 2;
float screenY = Screen.height / 2;
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(new Vector3(screenX, screenY));
if(Physics.Raycast(ray, out hit))
{
transform.LookAt(hit.point);
}
}
}
But, for some reason, this method makes my weapon rotate like this: [Here][1] (gyazo is a screenshot tool). As you can see , it doesn't rotate towards my hitpoint at all, but it rotates somwhere else entirely. Does anybody know how i can make my weapon rotate so that i can shoot towards the middle of my screen? Obviously instructions are enough, thanks!
If any additional information is needed, please let me know.
[1]: https://gyazo.com/0a491ac30686fb78d69736e096457b52
Comment
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Bullet flies away for no apparent reason. 1 Answer
Swapping the Position of an element on certain conditions 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer