How to rotate camera around player when idle, but control player rotations when moving?
Hello,
I am new to Unity and to scripting in general, I have been trying for a couple of days to achieve this, and after following many tutorials I can now do the following:
1- Control the player using keyboard and player turns around when the camera turns around (mouse X) when player is moving.
2- Player does not turn with camera when idle (and that's what I want).
What I am trying to add now is rotate the camera around the player when in idle without rotating the player himself. but continue to follow the camera when the player starts moving again.
Note: I have the main camera attached to the player as a child.
Here is the player control script attached to the player:
using UnityEngine;
using System.Collections;
public class PentuControls : MonoBehaviour {
static Animator anim;
private float speed = 0.1F;
public float rotationSpeed;
public float runSpeed;
public float walkSpeed;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
float translation = Input.GetAxis ("Vertical") * speed;
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if (Input.GetKey (KeyCode.LeftShift)) {
speed = runSpeed;
//Running
if (Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
else if (Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", true);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
}
//Walking
else if (Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", true);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", true);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.Q))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", true);
anim.SetBool ("isIdleRight", false);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.E))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", true);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.Q) && Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", true);
anim.SetBool ("isIdleRight", false);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.W))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", true);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", true);
speed = walkSpeed;
}
else if (Input.GetKey(KeyCode.E) && Input.GetKey(KeyCode.S))
{
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", false);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", true);
speed = walkSpeed;
}
else
{
//Is Idle
anim.SetBool ("isWalking", false);
anim.SetBool ("isRunning", false);
anim.SetBool ("isIdle", true);
anim.SetBool ("isIdleLeft", false);
anim.SetBool ("isIdleRight", false);
}
//var translation = Input.GetAxis ("Vertical") * speed;
//var rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
// transform.Translate (0, 0, translation);
// transform.Rotate (0, rotation, 0);
if (Input.GetButtonDown ("Jump"))
{
anim.SetTrigger ("isJumping");
}
}
}
And here is the mouse and camera control script attached to the player as well:
using UnityEngine;
using System.Collections;
public class MouseAimCamera : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;
public float turnSpeed;
void Start() {
offset = target.transform.position - transform.position;
}
void LateUpdate() {
if (Input.GetKey (KeyCode.W)) {
float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
target.transform.Rotate (0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler (0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt (target.transform);
}
if (Input.GetKey (KeyCode.S)) {
float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
target.transform.Rotate (0, horizontal, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler (0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt (target.transform);
}
}
}
Since your current camera control script is called "$$anonymous$$ouseAimCamera", I was wondering if you prefer to put the code about "rotate around player when idle" in the same script or not? For now I think maybe it's a good idea to create another script that does this job. For me it makes things simpler :) But it's up to you.
 
Translate and Rotate https://unity3d.com/learn/tutorials/topics/scripting/translate-and-rotate
 
Currently is your Hierarchy like this?
Player
â”” $$anonymous$$ain Camera
If you change it to
Player
â”” Camera Holder
â”” $$anonymous$$ain Camera
Set "Camera Holder"s local position 0, 0, 0. By rotating "Camera Holder", you can make "$$anonymous$$ain Camera" rotate around "Player".
 
What's left is to come up a way to enable this camera rotation script when idle, and disable it when not idle.
 
Enabling and Disabling Components https://unity3d.com/learn/tutorials/topics/scripting/enabling-and-disabling-components