• 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 LoganTylerLT · May 15, 2018 at 12:47 AM · scripting problemscripting beginnerscript error

Why do I only have left/right mouse movement

Keep in mind that only been looking into coding this past week and I'm not sure on how to make everything work for a simple first PersonController/Camera

I've been reading on how to create the code and I am the closest I've been on making it work using this.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class FPController : MonoBehaviour {
 
     public float speed = 5f;
 
     private Transform cam;
     private Rigidbody rb;
     private Vector3 velocity = Vector3.zero;
     private float mouseSensitivity = 250f;
     private float verticalLookRotation;
     
     //use this for initialization
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         cam = Camera.main.transform;
     }
 
     //update is called once per frame
     void Update ()
     {
         float xMov = Input.GetAxisRaw("Horizontal");
         float yMov = Input.GetAxisRaw("Vertical");
         float zMov = Input.GetAxisRaw("Jump");
 
         Vector3 movHor = transform.right * xMov; // (1, 0, 0,)
         Vector3 movVer = transform.forward * yMov; // (0, 0, 1,)
         Vector3 movUp = transform.up * zMov; // (0, 1, 0,)
         velocity = (movHor + movVer + movUp).normalized * speed;
 
         transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * Time.deltaTime * mouseSensitivity);
         verticalLookRotation += Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSensitivity;
         verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60, 60);
         cam.localEulerAngles = Vector3.left * verticalLookRotation;
     }
 
      private void FixedUpdate()
     {
         if (velocity != Vector3.zero)
         {
             rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
         }
     }
 }

After following The video on Coding First Person Controller

After following along and Checking over the code multiple times I see this in my console alt text

If anyone that knows a bit more about this than I do could explain what I did wrong and how to fix it would make me very grateful! Thank you in advance!

consoleerror.png (9.3 kB)
Comment
Add comment · Show 1
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 LoganTylerLT · May 15, 2018 at 01:30 PM 0
Share

Ive just noticed looking back at the code is that i dont have a horizontal axis rotation and may be the cause of me no being able to look up

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by vinilly · May 15, 2018 at 03:50 PM

The error is null reference - meaning camera.main.transform.(missing reference here);

Just some examples to set reference - Example;

     cam = camera.main.transform.position;
     cam = camera.main.transform.up;
     cam = camera.main.transform.left;
     cam = camera.main.transform.translate;
     cam = camera.main.transform.lookat;
     cam = camera.main.transform.right;
     cam = camera.main.transform.eulerangles;
     cam = camera.main.transform.rotation;
     cam = camera.main.transform.forward;
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 LoganTylerLT · May 17, 2018 at 01:29 AM 1
Share

I got it to work finally. Thank you for your help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

207 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

Related Questions

Displaying Text on Touch Event 0 Answers

Object reference not set to instance for my timer and my text 0 Answers

Getting weird error message!?!?!? 1 Answer

Making a game where objects need to be shot off of a surface. level complete when all objects are shot off. difficulty in scripting for level completion. 0 Answers

Error CS1519 1 Answer


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