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 satoshi · Sep 14, 2010 at 09:15 AM · rotatedragforward

rotation in dragging

I'm planning some applications. In that, I want to drag a gameObject and heading it forward. I uploaded just sample below. http://www015.upp.so-net.ne.jp/kira-kira/test01.html

1.firstly, Click the button on the left top "sub screen". 2.then, drag the red colored object in the small screen on the right bottom.

The red object follows to be heading forward. however, It seems like snapped every 45 degrees.

I want to rotate it more smoothly.

Please help me.

Below is the script on the red object.


var state:boolean = false; private var mainObj; private var orgPos:Vector3; private var noCollision:boolean = true;

function Start(){ mainObj = GameObject.Find("Cylinder01"); }

function OnMouseDown(){ state = true; }

function OnMouseUp(){ state = false; }

function OnTriggerEnter(collision : Collider) { noCollision = false; }

function OnTriggerExit(collision : Collider) { noCollision = true; }

function FixedUpdate () { if(state) { if(noCollision) orgPos = transform.position;

         var mainCamera = Camera.allCameras[1];

         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);

         mainObj.transform.position.x = ray.GetPoint(10).x; //.GetPoint(0.2)
         mainObj.transform.position.z = ray.GetPoint(10).z;

         if(!noCollision)    transform.position = orgPos;

         var nowPos = transform.position;
         relativePos = nowPos - orgPos;
         Debug.Log(relativePos);

         transform.rotation.eulerAngles.z = Vector3.Slerp(transform.forward, relativePos, Time.deltaTime).z;

         if (Vector3.Distance(relativePos, Vector3(0,0,0)) > 0 )
         {

             transform.LookAt(transform.position - relativePos);

         }
 }

}


Thanks a lot.

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
0

Answer by Bampf · Sep 14, 2010 at 01:18 PM

Some suggestions.

1) The Slerp is probably doing you no good, because the next FixedUpdate you are just going to overwrite the rotation again.

In fact it might part of the problem, because Time.deltaTime is always going to be close to zero, so you are basically just going to set the rotation to something close to the current forward vector.

Try setting the rotation to the final rotation you desire directly, without any Slerp. Once that's correct you can work on making the transition look nice.

2) This is very minor, but note that the following line:

if (Vector3.Distance(relativePos, Vector3(0,0,0)) > 0 )

is equivalent to the following but not as clear:

if (relativePos.magnitude > 0)

If this was being used many times per frame you could replace that with

if (relativePos.sqrMagnitude > 0)  // more efficient since you don't need the actual distance

or for that matter

if ( nowPos != orgPos ) // probably the most efficient

3) I'm wondering if the LookAt call and the setting of the Euler z rotation are interfering with each other. Try just one or the other.

4) General rotation tips:

  • Learn quaternions; they are generally superior to Euler angles. You don't have to understand how they work, just treat them as a black box representation of your object's rotation.
  • Gizmos.DrawRay and Gizmos.DrawLine are useful for visualizing vectors that you are trying to debug.
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 satoshi · Sep 14, 2010 at 03:51 PM

Thank you for your reply.

I have some mistake on my code.

so I simplyfied my code like below.

=========================================================

function FixedUpdate () {

if(state)

{ if(noCollision) orgPos = transform.position;

     var ray = mainCamera.ScreenPointToRay (Input.mousePosition);

     transform.position.x = ray.origin.x;
     transform.position.z = ray.origin.z;

     if(!noCollision)    transform.position = orgPos;

     var nowPos = transform.position;    

     if (nowPos != orgPos)
     {
         transform.LookAt(orgPos);
     }

}

}

=========================================================

But, I have still same problems of rotation. Please check my latest sample.

http://www015.upp.so-net.ne.jp/kira-kira/test02.html

I think... the position from "ScreenPointToRay (Input.mousePosition)" causes this problem??

Thanks.

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

gameobject stops moving correctly when rotating 1 Answer

Mouse Controller 1 Answer

Rotate disk from above on touch to choose ball direction. 0 Answers

Change characters direction using camera view 0 Answers

movement doesnt stop when jump pad is tapped at the same time 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