- Home /
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);
}
}`
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.
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.
Your answer
Follow this Question
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