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 Todd · Oct 07, 2010 at 06:30 PM · javascriptmouseorbitmouseorbit

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?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · 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 Todd · Oct 07, 2010 at 06:52 PM 0
Share

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.

avatar image Loius · Oct 08, 2010 at 03:18 AM 1
Share

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

avatar image
2

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.

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

No one has followed this question yet.

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


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