Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Design.PierreFX · Sep 15, 2012 at 06:48 PM · c#errorcrashfreezecontrols

C# Drag rotation code crashing unity with no errors.

Hey,

I'm trying to implement the drag rotation script from this thread: http://forum.unity3d.com/threads/13440-Physics-Input-Questions in C# rather than java, but every time I try to play it unity crashes as soon as I click on my sphere. There are no compiling errors, no error messages, it just freezes... Tried a bunch of stuff, but I can't seem to find what's crashing unity. Maybe I just need to take my eyes off the code for a little bit. In the meantime, if anyone has any ideas and could lend a hand I'd really appreciate the help.

Here's my code:

 `   using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     //Make sure there is always a rigidbody
     [RequireComponent (typeof (Rigidbody))]
     [RequireComponent (typeof (SphereCollider))]
     
     public class Planet_Controls : MonoBehaviour {
         
         // Key press vars
         public float fRotationSpeed = 100.0F;
         public float fFriction = 1;
         public float fInterpolation = 1;
         
         // Drag rotate vars
         public int iNumberAverages = 3;
         private Quaternion qOriginalRot;
         private Quaternion qOffsetRot;
         
     
         
         // Use this for initialization
         void Start () {
         
         }
         
         void Awake() {
             iNumberAverages = Mathf.Clamp(iNumberAverages,1,iNumberAverages);
         }
         
         void OnMouseDown() {
             RaycastHit hit;
             Vector3 dir;
             
             //Stop spinning
             rigidbody.angularVelocity = Vector3.zero;
             
             //Record Initial Vars
             if(Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition),out hit)){
                 qOriginalRot = transform.rotation;
                 dir = hit.point - transform.position;
                 qOffsetRot = Quaternion.Inverse (Quaternion.LookRotation(dir));
                 Spin(dir);
             }
         }
         
         void Spin (Vector3 dir) {
             RaycastHit hit;
             List<Vector3> previousDirList;
             Vector3 currentDir = dir;
             
             previousDirList = new List<Vector3>();
             
             //init previousdir list
             for (int i = 0; i < iNumberAverages; i++){
                 previousDirList.Add(currentDir);
             }    
             //Make the object rotate with the cursor while we are grabbing it
             while (Input.GetButton("Interact") && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) {
                 //Remove 1st element of array
                 previousDirList.RemoveAt(0);
                 
                 //Add currentdir to the end
                 previousDirList.Add(currentDir);
                 currentDir = hit.point - transform.position;
                 transform.rotation = Quaternion.LookRotation (currentDir) * qOffsetRot * qOriginalRot;
             }
             
             //User let go of the mouse, so let it spin on its own
             Vector3 avgPreviousDir = Vector3.zero;
             
             for (int i = 0; i < iNumberAverages; i++) {
                 avgPreviousDir += previousDirList[i];
             }
             
             avgPreviousDir /= iNumberAverages;
             
             Kick(currentDir,avgPreviousDir);
         }
         
         void Kick (Vector3 r2, Vector3 r1){
             Vector3 linearVelocity;
                     
             //Calculate the angular velocity: omega = r x v / r^2
             linearVelocity = (r2-r1) / Time.deltaTime;
             
             rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
         }
         
         // Update is called once per frame
         void Update () {
             //Key press rotation system
                 //Grab inputs
                 float xrot = Input.GetAxis("XRot")*fRotationSpeed;
                 float yrot = Input.GetAxis("YRot")*fRotationSpeed;
                 float zrot = Input.GetAxis("ZRot")*fRotationSpeed;
                 //Keep it rotating every frame
                 xrot *= Time.deltaTime;
                 yrot *= Time.deltaTime;
                 zrot *= Time.deltaTime;
                 //Apply rotation to object
                 transform.Rotate(xrot,yrot,zrot);
         }
     }`
Comment
Add comment · Show 1
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 voncarp · Sep 16, 2012 at 01:02 AM 0
Share

Unity often gives you the courtesy of crashing on infinite loops. So I suspect it may be crashing during the while part of your code. It looks like you might just be able to change the while to an if statement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ThermalFusion · Sep 16, 2012 at 12:59 AM

 while (Input.GetButton("Interact") && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) {

will loop infinitely once its true, causing the crash.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# script makes unity freeze on ArrayList.Add() 3 Answers

Unity not picking up errors until after reopening 0 Answers

Multiple Cars not working 1 Answer

Unity freezes on play 1 Answer

Victory menu is not working HELP 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