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 erikzn · Jan 23, 2014 at 09:01 PM · raycastmouseplanemouselooklook

Plane.Raycast for mouselook won't work in positive y-axis

Hi. I'm trying to make a tank shooter game, and I have a small problem with my turret mouselook.

I use Plane.Raycast to rotate the turret towards the mouse position, but it only works for sideways and downwards rotation. When I move the cursor above the middle of the screen (y-axis), the tank's turret won't rotate towards the mouse anymore.

Here is the code I'm currently using:

 public class PlayerTankController : MonoBehaviour {
     private Transform turret;
     private Transform bulletSpawnPoint;
     Plane ground;
     
     void Start() {
         turret = transform.Find("Turret");
         bulletSpawnPoint = turret.GetChild(0).transform;
         ground = new Plane(Vector3.up, transform.position);
     }
 
     void Update() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         float dist = 0.0f;
         
         if(ground.Raycast(ray, out dist)) {
             Vector3 clickPoint = ray.GetPoint(dist);
             turret.LookAt (clickPoint);
             bulletSpawnPoint.LookAt(clickPoint);
         }
     }

Do anyone know how to fix this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 23, 2014 at 09:31 PM

I'm going to guess this is a first-person perspective from the turret point-of-view. If so, above a certain point, your ray does not intersect the plane, so the Plane.Raycast() fails. Depending on the nature of your game, you have a couple of choices. First, if you only want the turret to shoot when there is something in the crosshairs, then replace your Plane.Raycast() with a Physics.Raycast(). If you want the tank to shoot at all the time, then when the Plane.Raycast() fails, you want to do a fallback calculation for the aim point. Pick a distance from the camera for this fallback point, then use Camera.ScreenToWorldPoint() based on this distance. Be sure you set the 'z' of the point passed to this function to the fallback distance.

The code might look like:

    Vector3 clickPoint = Vector3.zero;
    if(ground.Raycast(ray, out dist)) {
      Vector3 clickPoint = ray.GetPoint(dist);
      }
    else {
        clickPoint = Input.mousePosition;
        clickPoint.z = defaultDistance;
        clickPoint = Camera.main.ScreenToWorldPoint(clickPoint);
    }

   bulletSpawnPoint.LookAt(clickPoint);
   turret.LookAt (clickPoint);
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

18 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

Related Questions

Unity Android Movement Problem 0 Answers

draw line between mouse and gameobject on flat plane 0 Answers

smooth raycast LookAt ? 1 Answer

Intersection point of mouse with ZX plane 1 Answer

Mouse plane does not detect height 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