Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by awmiller.andros · Jun 21, 2011 at 03:14 AM · c#crash

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
}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Joshua · Jun 21, 2011 at 03:20 AM

Because it get's stuck in the while loop.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by awmiller.andros · Jun 21, 2011 at 03:22 AM

oh god... i feel stupid

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Jun 21, 2011 at 03:24 AM 0
Share

;) it's a mistake we've all made. In the future, please don't post answers as comments.

avatar image
0

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)); }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Jun 21, 2011 at 03:27 AM 0
Share

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.

avatar image ckfinite · Jun 21, 2011 at 03:31 AM 0
Share

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.

avatar image Joshua · Jun 21, 2011 at 03:36 AM 0
Share

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 ;)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is Unity crashing every time I press play in the editor after switching scenes? 0 Answers

When using visual studio, and saving code outside of unity, it wrecks my projects. Why and how can I fix my old pojects? 2 Answers

Game doesnt start after adding Admob 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges