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 Scorpox · Mar 23, 2015 at 11:48 PM · rotationplanemouselooktank

Tank turret rotation problem

Hi everyone. I'm trying to make a tank with a rotating turret. I have this script in which I make a plane and use it to raycast the mouse position and tell the cannon to LookAt that position. The problem I have is that the plane I create (via scripting) is centered in (0,0,0) I detect the raycast but when I put the tank forward/backward/left/right, the turret can only LookAt forward/backward/left/right. How could I detect the raycast as if the center of the plane were right below my tank?

Here is the C# code:

 using UnityEngine;
 using System.Collections;
 
 
 public class TankController : MonoBehaviour 
 {
     public Transform turret;
     public int smooth;
 
     public float movSpeed;
     public float rotSpeed;
     Vector3 targetPosition;
 
     void FixedUpdate ()
     {
         MoveTurret();
 
         RotateTurret();
     }
 
     void RotateTurret()
     {
         Plane playerPlane = new Plane(transform.up, turret.localPosition);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float hitDist = 0.0f;
 
 
         if(playerPlane.Raycast(ray, out hitDist))
         {
             print(hitDist);
             Vector3 targetPoint = ray.GetPoint(hitDist);
             Quaternion targetRotation = Quaternion.LookRotation(targetPoint - turret.localPosition);
             turret.rotation = Quaternion.Lerp(turret.rotation, targetRotation, Time.deltaTime * smooth);
         }
     }
 }


I hope anyone can help me with this. Thanks a lot.

Comment
Add comment · Show 6
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 Cherno · Mar 23, 2015 at 11:57 PM 0
Share

I don't understand how the placement of the plane would make any kind of difference for the rotation of the turret.

Also, why don't use use a Raycast hit for the raycast?

 Plane playerPlane = new Plane(transform.up, turret.localPosition);
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          RaycastHit hit = new RaycastHit();
  
  
          if(playerPlane.Raycast(ray, out hit))
          {
              Vector3 targetPoint = hit.point;
              Quaternion targetRotation = Quaternion.LookRotation(targetPoint - turret.localPosition);
              turret.rotation = Quaternion.Lerp(turret.rotation, targetRotation, Time.deltaTime * smooth);
          }
avatar image Scorpox · Mar 24, 2015 at 12:23 AM 0
Share

But Plane.Raycast's second parameter can only be a float, not as in Physics.Raycast... I tried at first this, but the problem then was that if my cursor was on a wall or anything else, the turret would look at that position (rotating its X axis). This is the reason why I tried a Plane.Raycast... Any idea? Thanks a lot for your quick response. :)

avatar image Cherno · Mar 24, 2015 at 12:31 AM 1
Share

Oh, I didn't even notice taht you didn't use Physics.Raycast. Well, you shuld definitely do that. You can avoid registering raycast hits on unwanted objects (like walls) by passing a Layer$$anonymous$$ask to the raycast function which only includes those layers that you want the ray to register. $$anonymous$$ake the ground layer different from the walls and then include only that ground layer in the Layer$$anonymous$$ask.

avatar image Scorpox · Mar 24, 2015 at 12:42 AM 0
Share

$$anonymous$$mm... Sounds great, I have never used Layer$$anonymous$$asks but I will investigate how to use them. I'll let you know if I can get it working. :)

avatar image Cherno · Mar 24, 2015 at 12:53 AM 1
Share

IT's simple. You just declare it and if it's public you can select the layers in it in the inspector from the drop-down list. You can also assign layers by script in the Start function:

 public Layer$$anonymous$$ask cursorHitLayer$$anonymous$$ask;
 
 cursorHitLayer$$anonymous$$ask|= (1 << Layer$$anonymous$$ask.NameToLayer("Ground"));

Then in the Raycast function you just include it as a parameter:

  if(playerPlane.Raycast(ray, out hit, $$anonymous$$athf.Infinity, cursorHitLayer$$anonymous$$ask)) {
 
 }

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

rotate object to face mouse cursor along global y axis 0 Answers

Mathf.Clamp is 'Sticky'? 1 Answer

Can someone help me fix my Character-Set up? 1 Answer

Player not rotating with camera 1 Answer

Simulating Mouse Input for AI 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