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 Ekta-Mehta-D · Nov 23, 2013 at 06:30 AM · rotationbulletlookatshoot

Fire Bullet To Mouse Position Problem

Hello everyone,

I am firing bullet to mouse position. And my bullet is translating towards mouse position correctly.

Code i have written is here :

 #pragma strict
 var bullet : Transform; // the bullet prefab
 private var spawnPt : GameObject; // holds the spawn point object
 
 function Start()
 {
 
 }
 
 function Update()
 {
      if(Input.GetMouseButtonDown(0))
      { 
          
          // only do anything when the button is pressed:
          var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          var hit : RaycastHit;
          if (Physics.Raycast (ray, hit, 100))
          {
             if (!spawnPt) spawnPt = GameObject.Find("RightGunTip");
             var projectile = Instantiate(bullet, spawnPt.transform.position, spawnPt.transform.rotation); 
             // turn the projectile to hit.point
             projectile.transform.LookAt(hit.point); 
             Debug.Log("rotation angel :" + projectile.transform.rotation);
             // accelerate it
             projectile.rigidbody.AddForce(projectile.transform.forward * 1000);
          }
      }
 }

Now problem is that my bullet object is Capsule game object. And i have given rotation -90 on z axis.

But when i am shooting the bullet , at that time because of the projectile.transform.LookAt(hit.point); , bullet gets rotation on y axis which i don't want. so what should i write for perfect rotation of bullet?

Thanks in advance for your support and help..

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 iwaldrop · Nov 23, 2013 at 08:30 AM 0
Share

Honestly, using a capsule just sounds weird. Why not use a sphere, or some other custom geometry and get rid of the unnecessary rotation?

If you're going to use the capsule though, all you have to do is change the direction that you're adding to it from forward to up/down.

Also, check it:

 Ray ray = camera.ScreenPointToRay(Input.mousePosition);
 GameObject bullet = GameObject.Instantiate(bulletPrefab, spawnPoint.position, Quaternion.LookRotation(ray.direction)) as GameObject;
 bullet.rigidbody.AddForce(bullet.transform.forward * bulletSpeed, Force$$anonymous$$ode.VelocityChange);

Either raycast and use GetComponent to do your damage, or don't Raycast. You don't need the hit point, because you're firing a bullet through space that is going to hit whatever it hits, and when it does (theoretically) do damage. :)

2 Replies

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

Answer by Moor · Nov 23, 2013 at 09:27 AM

alt text

like the image, make a Empty game object and child the capsule bullet, then add a sphere collider and rigidbody to that empty game object remove those from capsule, finally add this script to that empty game object,

 #pragma strict
 var bullet : Transform; // the bullet prefab
 private var spawnPt : GameObject; // holds the spawn point object
  
 function Start()
 {
  
 }
  
 function Update()
 {
      if(Input.GetMouseButtonDown(0))
      { 
  
        // only do anything when the button is pressed:
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 100))
         {
             if (!spawnPt) spawnPt = GameObject.Find("RightGunTip");
             var projectile = Instantiate(bullet, transform.position,   Quaternion.identity);         
 
             projectile.transform.rotation = Quaternion.LookRotation(ray.direction);
             projectile.rigidbody.AddForce(ray.direction * 1000);
         }
      }
 }



capture.png (276.4 kB)
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 Moor · Nov 23, 2013 at 09:28 AM 1
Share

or other simple way is make a bullet from maya like software, and use it with that script

avatar image Ekta-Mehta-D · Nov 23, 2013 at 12:06 PM 0
Share

Thanks sir. Its working now..

avatar image
0

Answer by highpockets · Nov 23, 2013 at 07:26 AM

The forward vector of your object is going to lookat the location of the vector or transform that you pass the function (make sure that you know what side the forward vector of your object is on ). It also rotates the Transform's up vector to the world up location, which is y up, top of your screen by default unless you change it. Example: lookat( lookAtMe, thisIsMyUpDirection ). Vector3.up is default for the up vector, which is the second and optional parameter, but you can make it Vector3.down y axis (left,right x axis,forward,back z axis).

Hopefully that helps. Let me know if you still have troubles

Comment
Add comment · Show 1 · 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 Ekta-Mehta-D · Nov 23, 2013 at 10:53 AM 0
Share

sir , what r u trying to say ? Sorry could not understand.

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

19 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

Related Questions

A node in a childnode? 1 Answer

I can't shoot up or down. HELP! 1 Answer

Shoot Bullet At Touch Position : 2D 1 Answer

RayCast Problem : 2d 1 Answer

Rotation Problem 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