- Home /
Question by
Sinci1 · Jul 22, 2017 at 04:36 PM ·
c#cameracamera-movement
*Please help, my first person camera goes all over the place after enabling weapon collider*
Hello, I have a first person script, and it work good, but however if i enable another collider, then it acts all crazy? also i have tried disabling the script of the first person camera, and then the crazy movement stops, but when i enable it again it goes again. Meaning that it not a collider issue. Here is a GIF of the problem And here is the script of the first person camera :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScanningRobotTestCamera : MonoBehaviour {
//ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
public const float Y_AXIS_MIN = 0.00f;
public const float Y_AXIS_MAX = 50.00f;
[Space]
[Header("ThirdPlayerCamStuff")]
public Transform LookAtTarget;
public Transform CameraTransform;
private Camera cam;
public float Speed;
public float distance;
public float currentX;
public float currentY;
public float sensetivityX;
public float sensetivityY;
public bool Once;
public bool SecondOnce;
//ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//FirstPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
[Space]
[Header("FirstPlayerCamStuff")]
public float ClampFirstPersonMin;
public float ClampFirstPersonMax;
public GameObject Arms;
public GameObject RobotModel;
public GameObject PlayerMdel;
public Vector2 MouseLook;
public Vector2 SmoothV;
public float sensitivity;
public float smoothing;
public Vector2 md;
public bool Escape;
public GameObject Character;
public GameObject NormalParent;
//FirstPersonCamStuff^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Space]
[Header("Other")]
public string ChangeCamsKey;
public int DiffrentCamsNumber;
public int NumberOfDiffrentCams;
public enum DiffrentCams{FirstPerson,ThirdPerson};
public DiffrentCams DiffCams;
// Use this for initialization
void Start () {
CameraTransform = transform;
}
void Update(){
if (Input.GetKeyDown(ChangeCamsKey)){
if (DiffrentCamsNumber == NumberOfDiffrentCams){
DiffrentCamsNumber = 0;
}
DiffrentCamsNumber += 1;
if (DiffrentCamsNumber == 1){
DiffCams = DiffrentCams.FirstPerson;
}
if (DiffrentCamsNumber == 2){
DiffCams = DiffrentCams.ThirdPerson;
}
}
//FirstPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
if(DiffCams == DiffrentCams.FirstPerson){
Arms.transform.parent = transform;
RobotModel.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
if(!SecondOnce){
// transform.localEulerAngles = Vector3.zero;
Arms.transform.localEulerAngles = new Vector3(0,0,Arms.transform.localEulerAngles.z);
SecondOnce=true;
}
Once=false;
gameObject.transform.parent = Character.transform;
gameObject.transform.localPosition = new Vector3(0,2.6399991f,0);
md = new Vector2(Input.GetAxisRaw("Mouse X"),Input.GetAxisRaw("Mouse Y"));
print (md);
md = Vector2.Scale(md,new Vector2(sensitivity * smoothing, sensitivity * smoothing));
SmoothV.x = Mathf.Lerp (SmoothV.x, md.x, 1f / smoothing);
SmoothV.y = Mathf.Lerp (SmoothV.y, md.y, 1f / smoothing);
MouseLook += SmoothV;
MouseLook.y = Mathf.Clamp (MouseLook.y, ClampFirstPersonMin,ClampFirstPersonMax);
Arms.transform.localPosition = new Vector3(-1.85f,-2,1.31f);
transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
Character.transform.localRotation = Quaternion.AngleAxis (MouseLook.x,Character.transform.up);
}//FirstPersonCamStuff^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
if(DiffCams == DiffrentCams.ThirdPerson){
Arms.transform.parent = PlayerMdel.transform;
RobotModel.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
Arms.transform.localPosition = Vector3.zero;
if(!Once){
MouseLook.y = 0;
Arms.transform.localEulerAngles = Vector3.zero;
Once=true;
}
SecondOnce=false;
gameObject.transform.parent = NormalParent.transform;
transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
// Character.transform.localRotation = Quaternion.AngleAxis (MouseLook.x,Character.transform.up);
if(Input.GetKey(KeyCode.Mouse1)){
print(Vector3.back);
currentY += Input.GetAxis("Mouse Y") * Speed;
currentX += Input.GetAxis("Mouse X") * Speed;
currentY = Mathf.Clamp (currentY, Y_AXIS_MIN, Y_AXIS_MAX);
}}//ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
// Update is called once per frame
void LateUpdate () {
//ThirdPersonCamStuff VVVVVVVVVVVVVVVVVVVVVVVVVVVV
if (DiffCams == DiffrentCams.ThirdPerson) {
Vector3 dir = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
CameraTransform.position = LookAtTarget.position + rotation * dir;
CameraTransform.LookAt (LookAtTarget);
}//ThirdPersonCamStuff ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
}
Please help i cannot continue because of this problem
Comment