- Home /
Camera variable problems
So basically I have code for player movement and making character look at the mouse cursor. I actually was pretty happy that It seemed to work, but unfortunately the Camera.main ('does not contain a definition for 'main')'. And later on, '(Camera does not contain a definition for 'ScreenPointToRay')'. I really hope someone will help me because I was struggling with 3D movement system which will take collisions into account for like 2 days. This code has worked in this tutorial youtube video.
Screenshot of my Unity editor and objects etc. Player script and PlayerController script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (PlayerController))]
public class Player : MonoBehaviour {
Camera viewCamera;
PlayerController controller;
void Start() {
controller = GetComponent<PlayerController> ();
viewCamera = Camera.main;
}
void Update() {
Vector3 moveInput = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
Vector3 moveVelocity = moveInput.normalized * walkSpeed;
controller.Move (moveVelocity);
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane (Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(ray,out rayDistance)) {
Vector3 point = ray.GetPoint(rayDistance);
Debug.DrawLine(ray.origin,point,Color.red);
//Debug.DrawRay(ray.origin,ray.direction * 100,Color.red);
controller.LookAt(point);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612150322im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i change a childs local position with a variable? 1 Answer
Multiple Cars not working 1 Answer
Camera issue (storing temp variable C# problem) 3 Answers
Distribute terrain in zones 3 Answers
What is wrong with my raycast 1 Answer