- Home /
Why Does This C# Code Do Nothing?
The following is the included MouseOrbit script tentatively converted to C#. This is the first time I've ever worked with C#, so my code may well not make any sense- if that's the case, just say so.
using UnityEngine; using System.Collections;
public class ORBITALMOUSE: MonoBehaviour { Transform target; double distance= 10.0f;
double xSpeed= 250.0f; double ySpeed= 120.0f;
double yMinLimit= -20; double yMaxLimit= 80;
private double x= 0.0f; private double y= 0.0f;
[AddComponentMenu("Camera-Control/Mouse Orbit")] partial class MouseOrbit { }
void Start (){ Vector3 angles= transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void LateUpdate (){ if (target) { x += Input.GetAxis("Mouse X") xSpeed 0.02f; y -= Input.GetAxis("Mouse Y") ySpeed 0.02f;
Quaternion rotation= Quaternion.Euler((float)y, (float)x, (float)0);
Vector3 position= rotation * new Vector3(0.0f, 0.0f, (float)-distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
} }
I've attached this code to a camera object and its target is a basic sphere. Can anyone tell me why this code seems to have no effect?
Answer by Loius · Oct 07, 2010 at 06:34 PM
Do you have to put the functions inside that "Partial class" definition? I see empty brackets and they make me sad, but I've never encountered "partial class" before so I could be being insane.
Check to make sure your xSpeed and ySpeed aren't zero, and that your mousex/y axes are set up properly and that you've assigned a target.
How do I check the values of my xSpeed and ySpeed? Normally, I would just print the values out to check them, but I don't think I can do that with Unity, can I?
I should have mentioned in the OP that I've never used Unity before, either.
You can write Debug.Log(almost anything) to print to Unity's console (which should pop up on its own; if it doesn't, double click the bar on the bottom of Unity). So you could Debug.Log("XSP: " + xSpeed + "; YSP: " + ySpeed);
Answer by skovacs1 · Oct 07, 2010 at 07:21 PM
This is the MouseOrbit script converted to Unity's mono (almost C#, but not quite):
using UnityEngine;
[AddComponentMenu("Camera-Control/Mouse Orbit")] public class MouseOrbit : MonoBehaviour { public Transform target; public float distance= 10.0f;
public float xSpeed= 250.0f;
public float ySpeed= 120.0f;
public float yMinLimit= -20;
public float yMaxLimit= 80;
private float x= 0.0f;
private float y= 0.0f;
void Start (){
Vector3 angles= transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void LateUpdate (){
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static float ClampAngle (float angle, float min, float max) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
The partial class isn't really hurting anything, but since it's empty, why is it there?
Your script is set to follow a target transform, but that transform is private and is never set, therefore your code in LateUpdate will never get inside your if statement and will never do anything.
Also, if you have the standard asset mouse orbit, it will also try to add itself as a component called Camera-Control/MouseOrbit. You should consider adding your custom mouselook under a different menu entry.
Your answer
Follow this Question
Related Questions
Trouble with moving the camera. 2 Answers
2d camera help! 0 Answers
when mouse is on a object do this 2 Answers
Mouse Dragging/Throwing Objects with the Mouse Like in Black & White (The Game) 1 Answer
Camera orbit around clicked position 0 Answers