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 shahan · Aug 04, 2014 at 03:55 AM · cameraraycastmouselookmouseorbit

How do I ray cast shoot to mouse-crosshair from mesh and not camera?

Here's a video of my very flawed mechanics, I've posted in the forum but I'm gonna ask here about a specific issue that occurs at 2:01 http://youtu.be/3s7YBz-8WXQ

Here is the script attached to camera which is a child object mouseorbiting around a player mesh the spawnPoint of the bullet prefabs is a child of the camera so the bullets shoot out from the camera lol. What i moved the spawn point to the mesh's hand or something it was aiming totally off and offset:

 using UnityEngine;
 using System.Collections;
  
 public class Shoot : MonoBehaviour {
  
   public AudioClip shootSound;
   public float shootForce = 2500;
   public GameObject spawnPoint;
   public GameObject bulletPF;
   public GameObject Character;
   private GameObject projectile;
   public bool isLaserState = false;
  
   void Update() {
     if(isLaserState){
       if(Input.GetMouseButton(0)) {
         audio.PlayOneShot(shootSound);
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Physics.Raycast(ray, out hit);//hit exits now
         projectile = Instantiate(
             bulletPF,
             spawnPoint.transform.position,
             spawnPoint.transform.rotation) as GameObject;
  
         Debug.DrawLine(spawnPoint.transform.position, hit.point);
  
         projectile.transform.LookAt(hit.point);
  
         Physics.IgnoreCollision(projectile.collider, Character.collider);
  
         projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
       }
     }else if (Input.GetMouseButtonDown(0)) {
       audio.PlayOneShot(shootSound);
       RaycastHit hit;
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       Physics.Raycast(ray, out hit);//hit exits now
       projectile = Instantiate(
           bulletPF,
           spawnPoint.transform.position,
           spawnPoint.transform.rotation) as GameObject;
  
       Debug.DrawLine(spawnPoint.transform.position, hit.point);
  
       projectile.transform.LookAt(hit.point);
  
       Physics.IgnoreCollision(projectile.collider, Character.collider);
  
       projectile.rigidbody.AddForceAtPosition(ray.direction * shootForce, hit.point);
     }
   }//end of Update
 }//end of class
Comment
Add comment · Show 2
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 robertbu · Aug 04, 2014 at 04:00 AM 0
Share

When the Raycasting comes from another object, you have to decide how you are going to handle the triangulation.

alt text

Image the hex is the camera with the red line the Ray from the mouse position. As you can see there are an infinite number of possible positions along the red line that the third party (blue triangle) can select as the aim point. So you can 1) pick a distance from the camera to calculate the point, or 2) you can Raycast and find a point and then do the raycast from the third party to that point. Or you can combine the two and use a distance if the raycast fails.

triangulate.png (14.0 kB)
avatar image shahan · Aug 04, 2014 at 05:01 AM 0
Share

right! so i actually moved the raycast origin to the spawn point, and it works somewhat now. http://youtu.be/C35uRioxTpI

I still lookat(hit) but now i : projectile.rigidbody.velocity = projectile.transform.forward * shootForce;

ins$$anonymous$$d of what's on line 50. but now when i shoot off into nothing. it veers off into the x axis?? ... ohh wait do i have to use ray.direction??

1 Reply

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

Answer by shahan · Aug 04, 2014 at 07:29 AM

So I actually just got it to work with some help from #unity3D on IRC. credit definitely goes to user Dezoaan on #unity3D

this code fixes it: It has everything to do with the new variable added in called 'aimPoint' which is a vector3 that if hit.point = (0.0,0.0,0.0) then aimPoint = to a point 100 units into the ray. so now you have a point to lookAt no matter what!

Here's a youtube video of it working. notice how spawnPoint is placed in front of the model now. resembling some sort of gun shooter or bullet emitting area. http://youtu.be/Ivtx2oykB1g

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
 
   public AudioClip shootSound;
   public float shootForce = 2500;
   public GameObject spawnPoint;
   public GameObject bulletPF;
   public GameObject Character;
   private GameObject projectile;
   public bool isLaserState = false;
   int ammo = 20;
 
   void Update() {
     if(isLaserState){
       if(Input.GetMouseButton(0)) {
 
         audio.PlayOneShot(shootSound);
 
         RaycastHit hit;
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//straight ray to mouse position
 
         Vector3 aimPoint;//hit point placeholder
 
         if (Physics.Raycast(ray, out hit, 100)){//where the ray hits, so draw from anywhere to THIS = raycast from new THAT 
 
           aimPoint = hit.point;
 
         } else {                                        //if  ray doesn't hit anything, just make a point 100 units out into ray, to referece
 
           aimPoint = ray.origin + (ray.direction * 100);//aimPoint is some point 100 unitys into ray line
 
         }
 
         projectile = Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
 
         projectile.transform.LookAt(aimPoint);          //fixes when hit point was = (0,0,0);
 
         Debug.DrawLine(spawnPoint.transform.position, aimPoint);
 
         Physics.IgnoreCollision(projectile.collider, Character.collider);
 
         projectile.rigidbody.velocity = projectile.transform.forward * 80;//this plus the LookAt aimpoint sends a bullet on the correct ray
 
         ammo = 20;
       }
       // END OF LASERSTATE SHOOTING
     }else if (Input.GetMouseButtonDown(0) && (ammo > 0)) {//normal shooting, since button down
 
       audio.PlayOneShot(shootSound);
 
       ammo--;
 
       RaycastHit hit; //hit exists now
 
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
       Vector3 aimPoint;
 
       if (Physics.Raycast(ray, out hit, 100)){//same amazing check, credit to Deozaan #Unity3D on IRC
 
         aimPoint = hit.point;
 
       } else {
 
         aimPoint = ray.origin + (ray.direction * 100);
 
       }
 
       projectile = (GameObject)Instantiate( bulletPF, spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
 
       Debug.DrawLine(spawnPoint.transform.position, aimPoint);
 
       Debug.Log("this is hit point: " + hit.point);
       //so it is 0,0,0 when i hit nothing
       Debug.Log("this is null hit: " + aimPoint);
 
       projectile.transform.LookAt(aimPoint);//send it on the ray
 
       Physics.IgnoreCollision(projectile.collider, Character.collider);// don't hit character
 
       projectile.rigidbody.velocity = projectile.transform.forward * 80;
     }
 
   }//end of Update
 
 }//end of class
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with Camera controls for custom third person controller 0 Answers

Make the terrain ignore Raycast if in between Camera and Player? 1 Answer

Raycast Object Selection 3 Answers

MouseLook movement diagonal stutters... 0 Answers

Mouse Orbit after changing Position sets camera back to last position 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