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 LeftyTwoGuns · Oct 18, 2013 at 02:25 AM · rotationraycastblenderaxislookrotation

How does LookRotation work in relation to ray cast?

I have a simple look at mouse cursor script that I'm trying to use to aim a turret by rotating on the Z-axis. Except I'm having some troubles with getting it to rotate correctly. Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class LookAtMouse : MonoBehaviour {
 
     public float speed;
  
     void FixedUpdate () 
     {
         
         Plane playerPlane = new Plane(Vector3.up, transform.position);
  
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
         
         float hitdist = 0.0f;
         
         if (playerPlane.Raycast (ray, out hitdist)) 
         {
             
             Vector3 targetPoint = ray.GetPoint(hitdist);
             targetPoint.z = 0;
  
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
  
             
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time);
         }
     }
 }

The turret is just a simple place holder I made in Blender- one horizontal cylinder with two barrels attached pointing up. The purpose of the script is to make the barrels aim at the mouse cursor while only rotating on the Z-axis. I can't get that to happen no matter how I change the transform position of the plane.

I could be wrong, shouldn't Vector3.up tell the cylinder to look a long the Y-axis? That is the direction that my barrels are pointing and shouldn't setting targetPoint.z = 0 lock rotation to the Z-axis? I think the problem is with the transition from Blender to Unity- from Z-up to Y-up, as the cylinder rotates but the barrels are never looking at the mouse.

Comment
Add comment · Show 3
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 robertbu · Oct 18, 2013 at 04:57 AM 0
Share

1) Given your geometry here, I have to assume this a top down game. If it isn't, then this code will not work.

2) To eli$$anonymous$$ate the mesh from the issue. Take a cube and make it long on the 'z' sides and narrow on the 'x' and 'y' to act as your barrel. Typically when testing alignment issues before I have meshes, I'll put a small sphere on the front side so I can see which is forward. Use this to test your code. If this works, and your Blender models don't, then you know the issues is the models.

3) 'targetPoint.z=0' will only work if your model is at 'z' = 0. If it isn't, then you need to set targetPoint.z to transform.position.z.

4) For your LookRotation(), you want to specify the optional 'up' parameter, and you want to use Vector3.forward. So it will be: '`Quaternion.LookRotation(targetPoint - transform.position, Vector3.forward);`'.

5) Though is probably a given, this code only works if the turret is axigned with the world 'X' and 'Y' axes.

avatar image LeftyTwoGuns · Oct 18, 2013 at 11:42 PM 0
Share

I've tried your testing method and it appears to be the script. $$anonymous$$y game is a 2D perspective but it isn't top down. It's front perspective, like a 2D platformer- Y is up, X is horizontal, Z is the 3D space. So does that mean I'm misunderstanding the use of this script?

And to tbkn, thank you very much for sharing your script but it gives me the error: "NewBehaviourScript.GetRotationDirection() is marked as an override but no suitable method found to override"

avatar image Tomer-Barkan · Oct 19, 2013 at 01:30 PM 0
Share

Removed the override keyword, like I said it was copied from my script in which the object inherited from another class...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Oct 18, 2013 at 06:47 AM

Here's the code I use to rotate the player object to the mouse:

   protected Vector3 GetRotationDirection() {
         Vector3 playerScreenPosition = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 mouseScreenPosition = Input.mousePosition;
         mouseScreenPosition.z = 0;
         Vector3 toMouse = mouseScreenPosition - playerScreenPosition;
         return toMouse;
     }
     
     public void Rotate() {
         Vector3 target = GetRotationDirection();
 
         // if turning 180 degrees, tell it to turn right otherwise it might turn on another axis than Z axis
         if (Vector3.Angle(transform.up, target) > 179) {
             target = transform.right;
         }
 
         Vector3 newUp = Vector3.RotateTowards(transform.up, target, rotateRate * Mathf.Deg2Rad * Time.deltaTime, 0);
         transform.up = newUp;
     }
     
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 Tomer-Barkan · Oct 19, 2013 at 01:31 PM 0
Share

Removed override keyword that was leftover from my own script.

avatar image Tomer-Barkan · Oct 28, 2013 at 06:14 AM 0
Share

Did it work?

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

15 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 avatar image avatar image avatar image avatar image

Related Questions

Inverse Rotation in Child Object 1 Answer

Orienting bones to Y forward 1 Answer

Rotating a 2D sprite to face a target on a single axis. 3 Answers

How can I rotate towards/look at a specific axis of a collided object? 2 Answers

Quaternion.LookRotation only on Y axis 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