- Home /
Question by
Dminic981 · Jun 17, 2020 at 09:45 AM ·
shootingnewbieoffsettop down shooterrotation detection
Rotation offset after hitting wall, help please,Rotating towards mouse unwanted offset after colliding with any wall in 3D
Hello, i hope someone can help me with this as i have not found a solution after trying to find one for about two hours. I'm trying to make a top-down shooter in 3D. So far i got everything working except this breaks the whole game:
This is the code responsible for rotating towards the mouse in 3D space, shooting, player movement and also crosshair placement:
crosshair = GameObject.Find("Crosshair").GetComponent<RawImage>();
rb = GameObject.Find("Player").GetComponent<Rigidbody>();
plr = GameObject.Find("Player").GetComponent<Transform>();
}
private void Update()
{
if (playerLives <= 0)
{
Time.timeScale = 0f;
SceneManager.LoadScene(0);
Time.timeScale = 1f;
}
}
// Update is called once per frame
void FixedUpdate()
{
hInput = Input.GetAxis("Horizontal") * 30f;
vInput = Input.GetAxis("Vertical") * 30f;
rb.AddForce(hInput, 0f, 0f);
rb.AddForce(0f, 0f, vInput);
if (Input.GetMouseButton(0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / 8f;
GameObject nB = Instantiate(bullet, plr.transform.position, plr.transform.rotation);
nB.transform.Translate(0f, 0f, +.6f);
nB.GetComponent<Rigidbody>().velocity = nB.transform.forward * 40f;
}
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up/5, Vector3.zero);
if (groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
plr.LookAt(new Vector3(pointToLook.x, plr.position.y, pointToLook.z));
}
crosshair.transform.position = Input.mousePosition;
I think I know what's wrong, the player object gets rotated a little bit because of the collision I just don't know how to reverse/prohibit that rotation. Sorry for the huge post. Thanks in advance!
before.png
(39.6 kB)
after.png
(60.1 kB)
Comment