Hello everyone.
Just for learning, i wanted to do a top down shooter of some sort.
I wanted to do as much as possible by my self, but ofcourse i need to follow some tutorials, that is why i dont know what i have done this time
Right now i was working on making rotation for my player, so he would always look towards my mouse (so i can also make him shoot that direction and stuff later)
I found some code in a video that i wanted to try, but i end up with an error message.
Here is the Code:
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.blue);
}
the error message i get is:
The name 'mainCamera' does not exist in the current context
I have not been able to find a way to call mainCamera.
I do have mainCamera in my game folder, but it cannot see it, and i dont know how to call it.
If anyone have an idea on how i can fix this, that would be awesome, thank you
Here is my whole player script, if you have any other improvements, or maybe another way to do this, than what i have found:
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
public float sprintSpeed;
void Start () {
moveSpeed = 3f;
sprintSpeed = 4.5f;
}
// Update is called once per frame
void Update () {
transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f, moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
if(Input.GetKey("left shift"))
{
transform.Translate(sprintSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, sprintSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
}
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.blue);
}
}
}
Thank you for your time