- Home /
Why does this make unity crash?
using UnityEngine; using System; using System.Collections;
public class RTScontroller : MonoBehaviour {
int lastScroll = 0; // last screen's scroll, used for velocity
float ZOOM_ANGLE = 6; // denominator of the radian degree used for zooming
float wheelVelocity = 0; // used for zoom velocity
float thisScroll; // float thisScroll = Input.GetAxis("Mouse ScrollWheel");
float horzKeys; // horizontal key movent relative to scrollSpeed
float vertKeys; // vertical movement relative to scroll
float yScroll; // = Input.GetAxis("Mouse ScrollWheel"); //store input, used primarily for +/-
float forwardScroll; // = Input.GetAxis("Mouse ScrollWheel");// same ^^
public const int MIN_Y = 40;
public const int MAX_Y = 400;
public const int scrollSpeed = 15; // Camera's movement speed
public const float VELOCITY_SCALE = 1; // used to scale zoom velocity
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
// mouse move method
// do movement by mousePosition
// screen Right
if (Input.mousePosition.x > Screen.width) { transform.Translate(Vector3.right * scrollSpeed); }
//screen left
if (Input.mousePosition.x < 0) { transform.Translate(Vector3.left * scrollSpeed); }
// screen up
if (Input.mousePosition.y > Screen.height) { transform.Translate(Vector3.forward * scrollSpeed); }
//screen down
if (Input.mousePosition.y < 0) { transform.Translate(Vector3.forward * -scrollSpeed); }
//keybord move method
// Do camera movement by keyboard
horzKeys = (Input.GetAxis("Horizontal") * scrollSpeed);
vertKeys = (Input.GetAxis("Vertical") * scrollSpeed);
transform.Translate(horzKeys, 0, vertKeys);
// Do zoom by mouse wheel
//thisScroll = Input.GetAxis("Mouse ScrollWheel");
velocityUpdate(); // Updates wheel velocity in order to continue acceleration of zoom feature
float rotationSpeed = ((float)Math.PI * (float)wheelVelocity) / ZOOM_ANGLE; // use wheelVelocity to get correct angle scale and direction
/* because the change in camera height is dependant on wheelVelocity, so must be the angle at which we rotate during the zoom.
if we multiply the angle by the velocity, the angle should rotate the same relative to the speed of the scroll*/
while ( transform.position.y > MIN_Y && transform.position.y < MAX_Y )
{
//yScroll = Input.GetAxis("Mouse ScrollWheel") * wheelVelocity;
forwardScroll = Input.GetAxis("Mouse ScrollWheel") * wheelVelocity;
transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity);
if (thisScroll > 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
if (thisScroll < 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
}
}
/*
RTScontroller.velocityUpdate() : currently private use : accelerated zoom feature for game camera takes an input reading from a mouse wheel ( -1 < x < 1 ) and increments the wheelVelocity complete stop is recognized as 2 frames of no movement use VELOCITY_SCALE to increase the ammount of acceleration */ void velocityUpdate() {
if ((lastScroll == 0) && (thisScroll == 0)) { wheelVelocity = 0; }// if no scroll, reset velocity to zero
else
{ //prioritize input decisions by clearly seperating no scroll option
if (thisScroll != 0)
{ // if thisScroll isnt zero, the following will compound velocity
if (lastScroll > 0 && thisScroll > 0) { wheelVelocity++; }; // if positive scroll, increase velocity
if (lastScroll > 0 && thisScroll < 0) { wheelVelocity = -1; }; // quickly switches to opposite scroll
if (lastScroll < 0 && thisScroll < 0) { wheelVelocity--; }; // if negative scroll, decrement velocity
if (lastScroll < 0 && thisScroll > 0) { }
}
else { wheelVelocity = lastScroll; } // if this scroll is zero, prepare to stop. used for soft deceleration
}
// end the funtion by adding a constant to scale velocity effect
wheelVelocity = wheelVelocity * VELOCITY_SCALE;
}
//end class
}
Answer by awmiller.andros · Jun 21, 2011 at 03:22 AM
oh god... i feel stupid
;) it's a mistake we've all made. In the future, please don't post answers as comments.
Answer by ckfinite · Jun 21, 2011 at 03:23 AM
This is the important bit:
while ( transform.position.y > MIN_Y && transform.position.y < MAX_Y )
{
//yScroll = Input.GetAxis("Mouse ScrollWheel") * wheelVelocity;
forwardScroll = Input.GetAxis("Mouse ScrollWheel") * wheelVelocity;
transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity);
if (thisScroll > 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
if (thisScroll < 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
}
Basically, this blocks the rest of program execution. Change it to this instead:
forwardScroll = Input.GetAxis("Mouse ScrollWheel") * wheelVelocity;
transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity, Input.GetAxis("Mouse ScrollWheel") * wheelVelocity);
transform.position.y = Mathf.Clamp(transform.position.y, MIN_Y, MAX_Y);
if (thisScroll > 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
if (thisScroll < 0) { transform.Rotate((Vector3.up * rotationSpeed)); }
That would remove the 'smoothed' translation he tries to get through the scrollwheel though. He'd probably be better of turning the while into an if, so he can scroll for several frames.
Well, it would only change anything when it reached the boundary. The speed of the scroll wheel would still cause a "smooth" transition. Anyway, the way to make it truly smooth would require Vector3.Slerp, functionality that the original while loop would not provide.
You're right, I misunderstood, this will work fine. Although the while is entirely obsolete since the code block will always only be run once.. so it's effectively an if loop already ;)
Answer by Molix · Jun 21, 2011 at 03:28 AM
It doesn't look like there's a way out of the while loop at the end of Update(). If there is no MouseWheel activity, the transform's y position will not change.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113316im_/https://answers.unity.com/themes/thub/images/avi.jpg)