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 TheShadyColombian · Oct 14, 2014 at 09:21 PM · raycastmousecursor

Raycast being weird

To be totally honest, i don't know much about ray casts (or anything). i actually copy and pasted a few bits and pieces together from the unity Nightmares tutorial project. I have a problem where it sometimes doesn't follow the cursor for a tiny little bit. the object on the floor layer has a few children and some colliders that stick out of the ground. if i change it's layer and put a flat plane on the floor layer, will this fix my problem? Here's the script I used, and sorry if it's a bit hard to find that part.

 using UnityEngine;
 using System.Collections;
 
 public class ExperimentalAim : MonoBehaviour {
     public float RotSpeed = 0f;
     private bool paused = false;
     public Rigidbody playerRigidBody;
     public float camRayLength = 100f;
     public int floorMask; 
 
     // Use this for initialization
     void Start () {
 
     }
 
     void Awake () {
         playerRigidBody = GetComponent <Rigidbody> ();
         floorMask = LayerMask.GetMask ("Floor");
     }
 
     void Turning ()
     {
         // Create a ray from the mouse cursor on screen in the direction of the camera.
         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         // Create a RaycastHit variable to store information about what was hit by the ray.
         RaycastHit floorHit;
         
         // Perform the raycast and if it hits something on the floor layer...
         if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
         {
             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
             Vector3 playerToMouse = floorHit.point - transform.position;
             
             // Ensure the vector is entirely along the floor plane.
             playerToMouse.y = 0f;
             
             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
             
             // Set the player's rotation to this new rotation.
             playerRigidBody.MoveRotation (newRotation);
         }
     }
 
     
     // Update is called once per frame
     void Update () {
 
         Turning();
 
         /*
 
         if(Input.GetKeyUp(KeyCode.Escape))
         {
             paused = !paused;
         }
 
         if (Input.GetKey (KeyCode.LeftArrow) && paused == false) {
         transform.Rotate (Vector3.down * RotSpeed);
         }
 
         if (Input.GetKey (KeyCode.RightArrow) && paused == false) {
         transform.Rotate (Vector3.up * RotSpeed);
         }
 
         if (Input.GetKeyDown (KeyCode.DownArrow) && paused == false) {
             transform.Rotate (Vector3.up * 180);
         }
 
         */
     }
 }
Comment
Add comment · Show 1
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 Fmressel · Oct 14, 2014 at 11:18 PM 0
Share

What is the thing that doesn't follow the cursor?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unimechanic · Oct 15, 2014 at 02:29 PM

To be totally honest, i don't know much about ray casts (or anything)

Our documentation and tutorials can help you, take your time to learn something amazing :)

http://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

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 TheShadyColombian · Oct 15, 2014 at 08:33 PM 0
Share

thanks, dude! although i just moved the ground to a different layer and set an invisible plane on the floor layer and it fixed it

avatar image TheShadyColombian · Oct 15, 2014 at 08:34 PM 0
Share

and do you know if i can use this in the final game?

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

30 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

Related Questions

Holding down the left mouse to continually damage an ai? 2 Answers

Invisible mouse script? 1 Answer

Attaching position of an object in game to position of cursor on screen 0 Answers

Disable/enable script and animation when you move your mouse cursor 1 Answer

I need help mimmicking mouse functinality without mouse events 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