How to modify this script to not rotate the camera when I rotate the player.
I want the camera to follow the player but I dont want the camera to rotate when the player is turning. This is the entire script :
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class SetupLocalPlayer2 : NetworkBehaviour {
[SyncVar]
string pname = "Player";
void OnGUI()
{
if (isLocalPlayer) {
pname = GUI.TextField (new Rect (25, Screen.height - 40, 100, 30), pname);
if(GUI.Button(new Rect(130,Screen.height - 40,80,30),"Change"))
{
CmdChangeName(pname);
}
}
}
[Command]
public void CmdChangeName(string newName)
{
pname = newName;
this.GetComponentInChildren<TextMesh> ().text = pname;
}
void Start ()
{
if (isLocalPlayer)
{
GetComponent<drive>().enabled = true;
Camera.main.transform.position = this.transform.position - this.transform.forward*10 + this.transform.up*8;
Camera.main.transform.LookAt(this.transform);
Camera.main.transform.parent = this.transform;
}
}
void Update()
{
if (isLocalPlayer)
this.GetComponentInChildren<TextMesh> ().text = pname;
}
}
And I want to change this part to not rotate the camera when I rotate the player around :
void Start ()
{
if (isLocalPlayer)
{
GetComponent<drive>().enabled = true;
Camera.main.transform.position = this.transform.position - this.transform.forward*10 + this.transform.up*8;
Camera.main.transform.LookAt(this.transform);
Camera.main.transform.parent = this.transform;
}
}
Sorry for my english, i'm quebecois and thank you very much. :)
Answer by ARKMs · Jun 24, 2016 at 07:40 PM
Try remove the last line of code:
Camera.main.transform.parent = this.transform;
This make the camera a child of your player, so this will take the transform and rotation of player. And maybe you will need another script for the camera, for follow the player.
Another point, is when you use 'LookAt', this rotate your camera too, so take care the rotation when use this.