Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by bobsi1 · Oct 09, 2016 at 02:07 PM · c#rotationerror messagetroubleshooting

3D top down player rotation error

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ElijahShadbolt · Oct 09, 2016 at 07:29 PM

The variable mainCamera is nonexistent.

One way to fix this would be to use Camera.main instead of mainCamera. This locates the first active camera in the scene with the tag "MainCamera" (the default scene camera).

The other way would be to write this line of code below [ public float sprintSpeed; ]

 public Camera mainCamera;

And then in the inspector set the reference to any camera in the scene.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bobsi1 · Oct 10, 2016 at 06:48 AM 0
Share

i will try this, thank you alot for the response.

avatar image
0

Answer by bobsi1 · Oct 10, 2016 at 06:54 AM

but the code still does not work for me, i want the charectar to always face the mouse, even tho the WASD shall still be the same direction on screen, and not change with him. For now my charectar does rotate with my mouse, but now when i fixed that (thanks to SublimeGamer) the wasd also changes with him. @SublimeGamer

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ElijahShadbolt · Oct 11, 2016 at 07:17 PM 0
Share

I modified the script so that the player character moves relative to the camera direction.

 using UnityEngine;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     public float moveSpeed;
     public float sprintSpeed;
 
     void Start()
     {
         moveSpeed = 3f;
         sprintSpeed = 4.5f;
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector3 planeNormal = Vector3.up;
         Plane groundplane = new Plane(planeNormal, Vector3.zero);
 
         float inputVert = Input.GetAxis("Vertical");
         float inputHorz = Input.GetAxis("Horizontal");
         float speed = Input.Get$$anonymous$$ey("left shift") ? sprintSpeed : moveSpeed;
 
         Vector3 right = Vector3.ProjectOnPlane(Camera.main.transform.right, planeNormal).normalized * speed * inputHorz * Time.deltaTime;
         Vector3 forward = Vector3.ProjectOnPlane(Camera.main.transform.forward, planeNormal).normalized * speed * inputVert * Time.deltaTime;
 
         transform.position += forward + right;
 
         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         float rayLength;
 
         if (groundplane.Raycast(cameraRay, out rayLength))
         {
             Vector3 pointToLook = cameraRay.GetPoint(rayLength);
             Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
 
             transform.rotation = Quaternion.LookRotation(pointToLook);
         }
 
     }
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

249 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues With Touch Rotation code 0 Answers

How can i rotate an object using LookAt with only 1 axis? 0 Answers

Unity ads initialize error 1 Answer

Know when the object is rotated? 1 Answer

Object rotates normaly while moving, then does zig zags? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges