• 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
Question by composemedia · Jun 29, 2017 at 07:32 PM · camerarotationcontrollerlock1st person

How to LOCK/STOP Rotation on Main Camera as child of Prefab?

Hi I'm making a Multiplayer (Networked) Game and would like to ac$$anonymous$$eve the following objectives:

  1. I would like to Instantiate my player at a Particular SpawnPoint ( Already Ac$$anonymous$$eved)

  2. I would like to use the Scenes Main Camera and place it be$$anonymous$$nd the Instantiated Local Player (10 Back, 25 High, and 75 Degree Angle looking down)

  3. I want my Local Player to be able to Rotate Left & Right (Already Ac$$anonymous$$eved)

  4. I DO NOT want my Main Camera to Rotate or Move at all Once it is placed be$$anonymous$$nd the Local Player.

I have most of t$$anonymous$$s working correctly and cant figure out how to ac$$anonymous$$eve the above. Please review my current code below:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityStandardAssets.CrossPlatformInput;

public class PlayerController : NetworkBehaviour {

 [Header ("Movement Variables")]
 public float moveSpeed = 5.0f;                 
 public float turnSpeed = 45.0f;                 
 [Header ("Camera Position Variables")]
 public float cameraDistance;         //The Distance the camera should be from the tank
 public float cameraHeight;         // The height off the ground that the camera should be
 //public float cameraAngle = 75f;     // The angle of the camera

 Rigidbody localRigidBody;        
 Transform mainCamera;            //reference to the scene's main camera
 Vector3 cameraOffset;            //a vector3 containing how far back and up the camera should be from the tank

 void Start () 
     {
     
     localRigidBody = GetComponent<Rigidbody> ();

     //set up the camera offset for future use
     cameraOffset = new Vector3(0f, cameraHeight, cameraDistance);

     //Find the main scene camera and move it into the correct position
     mainCamera = Camera.main.transform;
     MoveCamera ();

     }

 void FixedUpdate ()
 {
     if (!isLocalPlayer) 
         {
         return;
         }

     //Get the horizontal and vertical input. Note that we can get input for any platform using the CrossPlaformInput class
     float turnAmount = CrossPlatformInputManager.GetAxis("Horizontal");
     float moveAmount = CrossPlatformInputManager.GetAxis("Vertical");
             
     //Calculate and apply the new position
     Vector3 deltaTranslation = transform.position + transform.forward * moveSpeed * moveAmount * Time.deltaTime;
     localRigidBody.MovePosition (deltaTranslation);        

     //Calculate and apply the new rotation
     Quaternion deltaRotation = Quaternion.Euler (turnSpeed * new Vector3 (0, turnAmount, 0) * Time.deltaTime);
     localRigidBody.MoveRotation (localRigidBody.rotation * deltaRotation);
 
     MoveCamera ();   // T$$anonymous$$s Triggers the Camera Placement Method below

 }
 
 //T$$anonymous$$s method moves the camera to the correct spot be$$anonymous$$nd the player
 void MoveCamera ()
     {
     mainCamera.position = transform.position;    //position the camera on the tank
     mainCamera.rotation = transform.rotation;    //align the camera with the tank
     mainCamera.Translate (cameraOffset);        //move the camera up and away from the tank
     mainCamera.transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x + 70,transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);    //ANGLE OF THE CAMERA
     //mainCamera.LookAt(transform);                //make the camera look at the tank
 }    

}

Thank you in advance for your help.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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

154 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

Related Questions

Unity3D accelerometer camera rotation realistic controls 0 Answers

How do you disallow an angle from going to a certain range while rotating? 0 Answers

Limit Cam rotation 0 Answers

problem with first person camera 0 Answers

URP Camera doesn't allow transparency? OBS Overlay 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