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 yoger · Feb 05, 2013 at 03:35 PM · rotationmovementtopdownscreenpointtoray

Rotation while movement problem

Hello guys,

I've got simple movement function, which works just fine

 void Move() {
     if(Mathf.Abs(Input.GetAxis("MoveForward")) > 0){

         controller.SimpleMove(myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("MoveForward") * moveSpeed);

         animation.wrapMode = WrapMode.Loop;
         animation.CrossFade("run");
     }
     else {
         animation.CrossFade("idle", 0.4f);
     }
 }

but I wanted to attach another for rotation, so here it is

 void Rotate() {
 
     Plane playerPlane = new Plane(Vector3.up, transform.position);

     Ray ray = Camera.current.ScreenPointToRay (Input.mousePosition);

     float hitdist = 0.0f;

     if (playerPlane.Raycast (ray, out hitdist)) {

             

         Vector3 targetPoint = ray.GetPoint(hitdist);

             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.time);
     }        
 }

I call both in update and while just rotating or moving it works perfectly, but giving lots of that (not fot movement alone ofcourse):

 NullReferenceException
 UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:291)
 Movement.Rotate () (at Assets/Scripts/Actual Work/Movement.cs:30)
 Movement.Move () (at Assets/Scripts/Actual Work/Movement.cs:48)
 Movement.Update () (at Assets/Scripts/Actual Work/Movement.cs:23)

Real problem starts when I try to move and rotate in same time, my game object rotates like in steps (like 20-30 degrees at once), from time to time.

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 yoger · Feb 05, 2013 at 04:46 PM 0
Share

update on that, i just started it once again and it was running and rotating during movement just okay untill I stopped and started to run again. any clues?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 05, 2013 at 04:59 PM

Don't use Camera.current. It is only set in rendering callbacks. In Update it will always return null because during Update no camera is "active". See the docs on Camera.current

So use either:

  • Camera.main

  • or if you want to use a specific camera, create a public variable of type Camera and assign the camera in the inspector.

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 Wolfram · Feb 05, 2013 at 05:02 PM 0
Share

...and this is me being too slow again ^^

Didn't know that fact about Update(), though. Then again, never really used Camera.current.

avatar image Wolfram · Feb 05, 2013 at 05:04 PM 1
Share

Note that if your script is already attached to a camera (or one of its parents has a camera (not sure about that)), you could also use this.camera

avatar image
0

Answer by Wolfram · Feb 05, 2013 at 05:00 PM

NullReferenceException are always a problem and need to be dealt with, as any code following the line where it occurse is never executed, even if it's unrelated to the null object.

In your case it is cause by Camera.current being null at some point, which can happen in the editor since the internal SceneView camera also messes with that variable. As the docs mention, you shouln't generally use that.

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 yoger · Feb 05, 2013 at 06:52 PM

Thanks guys, I tried to get the way around returning null when using Camera.main, because I have misread about it. I tough it uses camera 'named' MainCamera, but it was just about setting its tag to it. Thanks for replies, now it works just perfect.

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

Top-Down Movement in Unity 1 Answer

Change character's Y rotation based on velocity. 1 Answer

Top down 2d movement without being affected by rotation 2 Answers

How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer

(sprite2D) Movement like this game... 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