- Home /
 
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);
         }
 
         */
     }
 }
 
              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
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
and do you know if i can use this in the final game?
Your answer
 
             Follow this Question
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