Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 21, 2019 at 04:21 PM by Fa6ex for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Fa6ex · Jun 20, 2019 at 11:55 PM · rotationraycastmousepositionray

3rd person 3D aiming

Hello.

Currently I'm trying to make an aiming system for a 3D, 3rd person game. My goal is to have the character aim the attack (a projectile, in this case) in a direction corresponding to the mouse position relative to the character's own position.
Example: if the mouse pointer is above and to the left of the character, I want the attack to be aimed forward, but with an upward and a leftward angle; this angle should increase the further the mouse pointer is relative to the character.

Unfortunately, I can't quite get this system to work. What am I doing wrong?

 public override void Aim(float range, Camera mainCamera, Transform firingPoint, LayerMask terrainMask)
     {
         Ray ray = mainCamera.ViewportPointToRay(Input.mousePosition);
         float targetRayDistance = Vector3.Distance(mainCamera.transform.position, firingPoint.position) / Mathf.Cos(Vector3.Angle(mainCamera.transform.forward, -firingPoint.forward)) + range; //this takes into the account the angle the camera is at relative to the character, and compensates
 
 
 
         if (Physics.Raycast(ray, targetRayDistance, terrainMask, QueryTriggerInteraction.Ignore)) 
         {
             firingPoint.rotation = Quaternion.LookRotation(ray.GetPoint(targetRayDistance), firingPoint.up); //if the Raycast collides with terrain, that means the projectile will too; I don't want that; therefore the projectile will be aimed horizontally instead
         }
         else
         {
             firingPoint.rotation = Quaternion.LookRotation(ray.GetPoint(targetRayDistance));
         }        
     }

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

  • Sort: 
avatar image
0

Answer by Fa6ex · Jun 21, 2019 at 04:20 PM

Alright, figured it out after a bit more searching. The answer is here https://stackoverflow.com/questions/29689617/mouse-based-aiming-unity3d but I adapted it slightly to fit what I was trying to do.

 Vector3 GetMousePositionInPlaneOfLauncher () {
     Plane p = new Plane(mainCamera.transform.forward, firingPoint.position);
     Ray r = mainCamera.ScreenPointToRay(Input.mousePosition);
     float d;
     if(p.Raycast(r, out d)) {
         Vector3 v = r.GetPoint(d);
         return v;
     }
 
     throw new UnityException("Mouse position ray not intersecting launcher plane");
 }
 
 firingPoint.LookAt(GetMousePositionInPlaneOfLauncher());
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

Follow this Question

Answers Answers and Comments

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

Related Questions

Player Chest rotates always in the same direction 0 Answers

Making an object follow the mouse in 3D world 0 Answers

Rotation of Object on single axis in direction of the mouse position 0 Answers

How do I make sure that a ray ALWAYS points straight down? 2 Answers

RayCasting shows wrong point 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