3d topdown shooter

hey I am trying to make a top down shooter and right now I am trying to get the player to face the mouse but it keeps spinning and freaking out
here’s the code
please help thanks!
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
public CharacterController characterController;
public float speed = 6;

private Camera MainCamera;

void Start ()
{
MainCamera = FindObjectOfType();
}
void Update()
{
Move();

Ray cameraRay = MainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;

if(groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.red);

transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}

private void Move()
{
float horizontalMove = Input.GetAxis(“Horizontal”);
float verticalMove = Input.GetAxis(“Vertical”);

Vector3 move = transform.forward * verticalMove + transform.right * horizontalMove;
characterController.Move(speed * Time.deltaTime * move);
}
}

You wrote you are trying to look at the camera. From your code it appears you are trying to look at the cursor.
Which is it?
There are a ton of tutorials for having something look at something else, or specifically at the cursor.

Also, for future posts:

  • Please use code tags to post code examples
  • Capslock and spamming !!!'s does not help getting attention. If anything there is an inverse relation.

Exactly. Calm down, it’s just 1s and 0s, nothing more.

Remember, with Unity at least half if not more of the problem can be in your scene setup, not just in code. The best code in the world isn’t worth a hill of beans if someone forgot to drag the right prefab in somewhere.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

i am looking for help and replies not grammar help and annoyance please comment if you can help if not than please go away

thank you this is helpful

I was asking you to clarify your question and also notifying you of how you could improve your posts in the future. If you react to that with such an attitude i dont think you will have a great time on this forum.
We cant read minds.

The help we offer here is free, and the least you can do is to make it easier for us to help you.
You are the one asking for help. So it’s your responsibility to make the people who are attempting to help you understand your problem. Not using capslock should be common sense, as it is usually considered “shouting” on the internet, and oftentimes seen as pushy or childish. As for code tags, the first sticky post on this subforum is about how to use them. It’s right next to the code of conduct, which you probably also did not read.
Doing so should be the first thing when you enter a new forum. It is not our fault if you cant be bothered to put in the minimal amount of effort before posting something - but why would you expect us to put more effort into an half-assed post like that then? That’s not how this works. It’s just disrespectul.

We have a lot of people like that, who do not use code tags in their first posts. And that’s absolutely fine. You nicely tell them, they do it better next time. But a reply like that? Sure, now i really want to help you.

2 Likes

thank you for explaining it nicely i am trying to get the player to face the mouse and I will fix these problems

i have fixed my problem, the camera was parented to the player causing it to glitch out

1 Like