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 CarbonTech Software · Jun 27, 2010 at 02:39 AM · rotationraycastspacelocalworld

Converting Raycasted Point to Local Space

Hello, I have a script that rotates a gun based on a raycast point (where the raycast hits the terrain or mesh). The script uses a "transform.lookat" function to rotate the gun-barrel to where it should point.

Plus, I only want the gun to rotate up and down (on the x axis locally). I found this code which works great but... there's a problem.

Transform.LookAt(Vector3(hit.point.x, transform.position.y, transform.position.z));

The problem is that the raycasted hit point is in the WORLD space while I need to rotate the gun around a LOCAL space. Does anyone know how to make it work (code is provided as a reference below):

var guntrans : Transform;
function Update () {
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit)) 
        {
  // Get your player object's position (could be any way, this is just one possible way)
         var playerT = GameObject.FindWithTag("RocketGun").transform;
  // Now you have where your player is and a 3D point the mouse is over
  Debug.DrawLine (playerT.position, hit.point);
  guntrans.LookAt(Vector3(hit.point.x, guntrans.position.y, guntrans.position.z));
        }
}

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
2
Best Answer

Answer by _Petroz · Jun 27, 2010 at 08:31 AM

If you want to be rotating up and down you should be looking at the Y coordinate, not the X.

I think that you're trying to match the height of the collision point but keep the rest of the rotation matching the parent. If that is the case you could do something like this:

pseudocode:

var parentLookAt = transform.position + parentTransform.forward;
lookat( Vector3(parentLookAt.x, hit.point.y, parentLookAt.z) );

Edit: I realized this code is wrong, it's not quite that simple, here is a modified version which should work:

pseudocode:

var lookAtRelative = hit.point - transform.position;
var lookAtMagnitudeXZ = Vector3(lookAtRelative.x, 0, lookAtRelative.z).magnitude;
var parentForwardXZ = Vector3(parentTransform.forward.x, 0, parentTransform.forward.z).normalize;
var parentLookAtXZ = transform.position + (parentForwardXZ * lookAtMagnitudeXZ);
lookat( Vector3(parentLookAt.x, hit.point.y, parentLookAt.z) );
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 Slin · Jun 27, 2010 at 10:29 AM

That is actually quite tricky, at least if I understand your problem correctly. After thinking about it a bit, I think that you can use the quaternions AxisAngle function. You should be able to get the angle as dot product between the current and the new look at vector and the axis is the cross product between your current up vector and the current look at vector. Should that do the job?

Edit: I missunderstood the previous answer, which I think works just as well and is much simpler :P

Comment
Add comment · Show 1 · 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 _Petroz · Jun 27, 2010 at 10:44 AM 0
Share

I can see how my answer could be unclear, i renamed 'parentDirection' to 'parentLookAt' I hope that's clearer. Do you think there is anything else that might make it easier to understand?

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

Apply a Rotation in World Space based on Quaternions 2 Answers

Is there a local method for Rigidbody.constraints? 0 Answers

Raycasting is in world direction, not local? 3 Answers

bone rotations changed in 4.3.2 inspector? 0 Answers

Why does the object alignment only work when the object isn't rotated? 2 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