How to LOCK/STOP Rotation on Main Camera as child of Prefab?
Hi I'm making a Multiplayer (Networked) Game and would like to achieve the following objectives:
I would like to Instantiate my player at a Particular SpawnPoint ( Already Achieved)
I would like to use the Scenes Main Camera and place it behind the Instantiated Local Player (10 Back, 25 High, and 75 Degree Angle looking down)
I want my Local Player to be able to Rotate Left & Right (Already Achieved)
I DO NOT want my Main Camera to Rotate or Move at all Once it is placed behind the Local Player.
I have most of this working correctly and cant figure out how to achieve 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 (); // This Triggers the Camera Placement Method below
}
//This method moves the camera to the correct spot behind 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.
Your answer
Follow this Question
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
Clamping Between Two Different Ranges 1 Answer
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
Unity3D accelerometer camera rotation realistic controls 0 Answers