- Home /
Question by
DavidJCole · May 02, 2020 at 07:53 AM ·
cameracamera-movementcamera rotate
How do I make a camera add torque on two different axis?
I have a player attached to an empty object, as well as a camera attached to an empty object. (labeled tag 1 and tag 2, tag 1 being attached to the player, tag 2 being attached to the camera.) In this specific movement system, tag 1 rotates to move tag 2 (and the camera) around the player, while tag 2 rotates up and down to make the camera look up and down. (I have also put a barrier in place so the camera doesn't go too high or low) The problem is, I cannot move the camera around the player when I have both functions activated, although when either of them are activated individually they work fine. My first thought was to split them up into separate scripts, but that would be clutter. Please excuse the variable names lmao.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraMovement : MonoBehaviour
{
public Transform cam;
public Rigidbody tag1rb;
public Transform tag1;
public Rigidbody tag2rb;
public Transform tag2;
public Transform littlespheredude;
public Vector3 offset;
public float xSensitivity;
public float ySensitivity;
private void FixedUpdate()
{
Cursor.lockState = CursorLockMode.Locked;
cam.position = tag2.position;
cam.rotation = tag2.rotation;
tag1.position = littlespheredude.position;
Debug.Log("Mouse = " + Input.GetAxis("Mouse X"));
xSensitivity = Input.GetAxis("Mouse X");
ySensitivity = -Input.GetAxis("Mouse Y") * 10;
LookHori();
LookVert();
}
void LookHori()
{
if (Input.GetAxis("Mouse X") > 0)
{
//Debug.Log("Mouse Right");
tag1rb.angularDrag = 0;
tag1rb.AddTorque(0, xSensitivity, 0);
}
else if (Input.GetAxis("Mouse X") < 0)
{
//Debug.Log("Mouse Left");
tag1rb.angularDrag = 0;
tag1rb.AddTorque(0, xSensitivity, 0);
}
else
{
tag1rb.angularDrag = 20;
}
}
void LookVert()
{
if (Input.GetAxis("Mouse Y") > 0)
{
//Debug.Log("Mouse up");
tag1rb.angularDrag = 0;
tag2rb.AddTorque(ySensitivity, 0, 0);
if (tag2.rotation.x <= -.2)
{
tag2rb.freezeRotation = true;
}
else
{
tag2rb.freezeRotation = false;
}
}
else if (Input.GetAxis("Mouse Y") < 0)
{
//Debug.Log("Mouse down");
tag1rb.angularDrag = 0;
tag2rb.AddTorque(ySensitivity, 0, 0);
if (tag2.rotation.x >= .2)
{
tag2rb.freezeRotation = true;
}
else
{
tag2rb.freezeRotation = false;
}
}
else
{
tag2rb.angularDrag = 20;
}
}
}
Comment