- Home /
Free Orbit Cam and Mouse Aim Cam aren't working together
I'm learning Unity3d about one month and this is my first question to community. If i did something wrong, forgive me.
I have scene that includes mecanim animated character and a camera. And I want to make that camera mouse controlled. This camera rotates and moves with these rules:
If the player isn't moving, camera must be "Free Orbit Cam" -means, easily moves around the player's Y axis. And looks the player of course. (That called free orbit camera right?)
If the player is moving (means, Vertical Axis and Horizontal Axis aren't equal to zero i think) camera must turn into a Mouse aim Camera and player has to move relative to the camera Axis'.
I always want to write code just myself and I tried to write that code, but 2 problems occured. Note, to make this simple I create another scene that includes just a cube and camera.
If WASD buttons are pressed, camera instantly changes it positon to offset position (i think so) I tried to move the line of offset code in the update() function camera is going crazy.
Player (or cube) isn't moving relative to the camera's direction.
Here is my code, thanks for your reply or advice.
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public GameObject mainCharacter;
public bool freeCamera = true;
public float cameraTurnSpeed = 5f;
private float cameraTurnAngle = 0;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = mainCharacter.transform.position - transform.position;
}
// Update is called once per frame
void Update () {
float mouseVertical = Input.GetAxis ("Mouse Y");
float mouseHorizontal = Input.GetAxis ("Mouse X");
float horizontalMove = Input.GetAxis ("Horizontal");
float verticalMove = Input.GetAxis ("Vertical");
Debug.DrawLine(mainCharacter.transform.position, transform.position, Color.red);
// If WASD not pressed, it is Free Camera
if (verticalMove == 0 || horizontalMove == 0) {
cameraTurnAngle = (cameraTurnAngle + (mouseHorizontal * cameraTurnSpeed)) % 360f;
Quaternion donusAcisi = Quaternion.Euler (0, cameraTurnAngle, 0);
transform.position = mainCharacter.transform.position - (donusAcisi * offset);
transform.LookAt (mainCharacter.transform);
}
if (verticalMove != 0 || horizontalMove != 0){
mainCharacter.transform.Rotate(0, mouseHorizontal * cameraTurnSpeed, 0);
float mainCharacterAngle = mainCharacter.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler (0, mainCharacterAngle, 0);
transform.position = mainCharacter.transform.position - (rotation * offset);
transform.LookAt (mainCharacter.transform);
}
}
void LateUpdate(){
}
}
Your answer
Follow this Question
Related Questions
Unity - Dancing Ball World camera following and rotating system 1 Answer
Would anyone teach me how to create camera movement and look controls? 2 Answers
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers
Screen is 2 cameras with each different properties. 1 Answer