Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: mechanicarts on September 24, 2014, 10:34:33 AM
-
Hello people. I've just gotten into PM, watching the videos etc. I'm about 1 hour (on and off) in PM and I'm already facing a game breaking problem. Having PM in the project (not even using it) breaks my SmoothMouseLook script. So I can't move the camera. I know I can use MouseLook from inside PM but the script I have works so much better for what I'm trying to do. Here is the script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SmoothMouseLook : MonoBehaviour
{
/*
This script is used to average the mouse input over x
amount of frames in order to create a smooth mouselook.
*/
//Mouse look sensitivity
public float sensitivityX = 2f;
public float sensitivityY = 2f;
//Default mouse sensitivity
public float defaultSensX = 2f;
public float defaultSensY = 2f;
//Minimum angle you can look up
public float minimumY = -60f;
public float maximumY = 60f;
//Number of frames to be averaged, used for smoothing mouselook
public int frameCounterX = 35;
public int frameCounterY = 35;
//Mouse rotation input
private float rotationX = 0f;
private float rotationY = 0f;
//Used to calculate the rotation of this object
private Quaternion xQuaternion;
private Quaternion yQuaternion;
private Quaternion originalRotation;
//Array of rotations to be averaged
private List<float> rotArrayX = new List<float> ();
private List<float> rotArrayY = new List<float> ();
void Start ()
{
//Lock/Hide cursor
Screen.lockCursor = true;
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
void Update ()
{
if (Screen.lockCursor) {
//Mouse/Camera Movement Smoothing:
//Average rotationX for smooth mouselook
float rotAverageX = 0f;
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
//Add the current rotation to the array, at the last position
rotArrayX.Add (rotationX);
//Reached max number of steps? Remove the oldest rotation from the array
if (rotArrayX.Count >= frameCounterX) {
rotArrayX.RemoveAt (0);
}
//Add all of these rotations together
for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {
//Loop through the array
rotAverageX += rotArrayX[i_counterX];
}
//Now divide by the number of rotations by the number of elements to get the average
rotAverageX /= rotArrayX.Count;
//Average rotationY, same process as above
float rotAverageY = 0;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotArrayY.Add (rotationY);
if (rotArrayY.Count >= frameCounterY) {
rotArrayY.RemoveAt (0);
}
for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {
rotAverageY += rotArrayY[i_counterY];
}
rotAverageY /= rotArrayY.Count;
//Apply and rotate this object
xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
}
private float ClampAngle (float angle, float min, float max)
{
if (angle < -360f)
angle += 360f;
if (angle > 360f)
angle -= 360f;
return Mathf.Clamp (angle, min, max);
}
}
How do I re enable it in PM? My Character Controller and Camera already have the script attached, but I guess PM somehow disables it.
Please use as little programming terminology as possible, so I beg you no " just void the update while ++'ing the public constructor". I am especially handicapped when it comes to even basic technology terms and I'm already over my head with the three little words PM revolves around (FSM) :)
-
The problem goes away when you delete Playmaker? Have you tried a new empty project and put the script in it? Restarted Unity?
-
The problem goes away when I delete PM and all refs from the project.
It won't work if there is even one element referring to it, eg the last time I forgot to delete PlayMakerGUI from the hierarchy, although there was nothing in the project.
It's reproducible. If I make a new project and attach the script to my controller, it will work. As soon as I import PM, the game will fail to lock the cursor, disabling my script.
-
PlayMakerGUI has a "Control Mouse Cursor" option. If you uncheck that it should allow your script to work normally... Let me know if it doesn't and I'll take a look...
-
Did change that, did follow the tutorials, congratulations, you have the best scripting plugin and support available for Unity :)