• 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 Arlei · Aug 19, 2012 at 06:55 AM · mouseclickshootspaceside

Can't shoot towards mouse click point

I'm making 3d space game from side-view, and i wanted to add some weapons that would shoot on x and y axis towards the point where the player click the mouse.I found something so i tried this:

 var hit  : RaycastHit ;
     if(Input .GetButtonDown ("Fire1"))
     {
          var ray : Ray  = Camera .main .ScreenPointToRay (Input .mousePosition );
          if(Physics.Raycast (ray, hit, Mathf.Infinity))
          {
              var shootProjectile : Rigidbody = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
              shootProjectile.AddForce (hit.point * projectileSpeed);
          }
 
     } 
 }

It shoots well if i click in to empty space, but when i click on object with collider, it shoots in very weird directions...I spend day and half finding a solution, but I didn't find it. I tried many things, just do'nt work. Can anyone help me? The best would be to post repaired code and explain why my code don't work :)

Comment
Add comment · Show 9
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 AlucardJay · Aug 19, 2012 at 07:05 AM 0
Share

Although your game is 2D, the colliders are 3D, so you are hitting the collider at a position that is above the 2D game plane. To fix this, lower all your colliders so the top of the collider is at the same y-value as your game plane.

i.e. if your 2D is on the X-Z plane, then all your colliders and gameObjects have to be positioned on the same Y value.

avatar image Arlei · Aug 19, 2012 at 07:09 AM 0
Share

I'm stupid... I didn't say that the game is 3d, just from side-view..

avatar image AlucardJay · Aug 19, 2012 at 07:25 AM 0
Share

then it could be how you calculate the AddForce. You should also store a local reference to the spawnpoint (at start if the spawnpoint doesn't change).

 var spawnPt : GameObject;

 function Start()
 {
     spawnPt = GameObject.Find("spawnPoint");
 }

Try

 shootProjectile.AddForce( (hit.point - spawnPt.transform.position) * projectileSpeed );
avatar image Arlei · Aug 19, 2012 at 07:34 AM 0
Share

It works!!! BUT still when i shoot on object with collider, it is veeery slow otherwise not. so I changed it to this:

shootProjectile.AddForce(( hit.point - spawnPt.transform.position )*projectileSpeed);

Now it shoot fast on objects with collider, but(and i don't know why) when i click on something without collider, it shoots incredibly fast..

avatar image AlucardJay · Aug 19, 2012 at 07:36 AM 0
Share

I just fixed the line :

 shootProjectile.AddForce( (hit.point - spawnPt.transform.position) * projectileSpeed );

I havn't tested this , but the idea is to find the direction to the collider from the spawnPoint. Just adding force at the position of the collider will give the same result no matter where the collider is.

Show more comments

1 Reply

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

Answer by Arlei · Aug 19, 2012 at 08:29 AM

Okay, I fixed it!

 var projectile : Rigidbody;
 var projectileSpeed : float;
 var spawnPt : GameObject;
 
 function Start()
 {
    spawnPt = GameObject.Find("spawnPoint");
 }
 
 function Update()
 {
 var hit  : RaycastHit ;
     if(Input .GetButtonDown ("Fire1"))
     {
          var ray : Ray  = Camera .main .ScreenPointToRay (Input .mousePosition );
          if(Physics.Raycast (ray, hit, Mathf.Infinity))
          {
              
              projectile = Instantiate(projectile, GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
              projectile.AddForce(( hit.point - spawnPt.transform.position )*projectileSpeed);
          }
     }
 }
 
 function FixedUpdate()
 {
     var velocity = projectile.velocity;
     if (velocity == Vector3.zero) return;
 
     var magnitude = velocity.magnitude;
     if (magnitude > projectileSpeed || magnitude < projectileSpeed)
     {
         velocity *= (projectileSpeed / magnitude);
         projectile.velocity = velocity;
     }
 }


I'm so happy! :D Who have the same problem, you can use this code :) thx for help AlucardJ :)

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 martimillion · Apr 18, 2014 at 09:21 AM 0
Share

I know it old ! But your code has an error! RaycastHit hit variable haven't been assigned

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Can't shoot towards mouse click point 0 Answers

click shooting how do i do it? 1 Answer

Converting button to mouse click 1 Answer

How to set up a space game with Evochron: Mercenary like controls? 0 Answers

Need Javascript to propel an object toward mouse click 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges