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 $$anonymous$$ · Jul 17, 2013 at 09:57 AM · c#rotationmouseshooting

[C#]Angle of shooting equale to mouse direction

Hi all! Im making a game similar to geometry wars http://www.youtube.com/watch?v=MwNeHPig0bw , so now i'm trying to making shooting like that, the angle of shooting is equale to the mouse direction, so how can i do this? I tried this, but it doesnt seem to work. Thanks for any help!

public class Bullet : MonoBehaviour {

 public float speed = 50f;
 
 // Use this for initialization
 void Start ()
 {
 
 }
 
 // Update is called once per frame
 void Update ()
 {

     transform.position = Vector3.MoveTowards(transform.position,Input.mousePosition,Time.deltaTime * speed);
 
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Narv · Jul 17, 2013 at 10:47 AM

You want to use Camera.ScreenPointToRay() to find a point in space where the the mouse hits, so you will use a Ray and a RaycastHit object for this. The doc for RaycastHit gives a good example you should be able to use for your script.

http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-point.html

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour {
     public float speed = 50f;
     void Update() {
         
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                transform.position = Vector3.MoveTowards(transform.position, hit.point, Time.deltaTim * speed);
             }
     
     }
 }


This moves something constantly in the direction of the mouse's position every frame and will move the object (bullet) in the direction of the mouse after it was fired. Without watching the video, if that isn't the destired effect you can get the hit.point value in Start() and then move towards hit.point every Update() function. (assuming this is Instantiated and Start() gets called when you "click" or press a button somewhere).

Comment
Add comment · Show 6 · 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 $$anonymous$$ · Jul 17, 2013 at 11:25 AM 0
Share

This works, but they are following mouse, i just need to move in mouse direction, and its 2d game.

avatar image Narv · Jul 17, 2013 at 11:50 AM 0
Share

you can change the x value (assu$$anonymous$$g you're 2D plane is setup on z,y.. if it's x,y then change the z value)

hit.point.x = 0; if everything is 0'd on the plane.

Is your scene centered in world space with the bottom left of the plane being at 0,0? Since mouseposition only gives a value from 0,0 to width,height. If things aren't centered properly it could throw off the direction in which it's trying to soot, hence the screenpointtoray will give you the point the mouse is at.. in geowars you have that background so I assume you would have a plane as your background, yes? so it will give you the point at which it hits the plane.

You could also try: Debug.DrawLine(start, end, Color.red) and have it draw a line every frame (per your code) to see where it thinks you are trying to shoot to and where your mouse is (debug line is drawn in scene window, not in game window).

avatar image Narv · Jul 17, 2013 at 11:52 AM 0
Share

Also, is your camera panning with the screen? this will also change what mousepoint is giving you as it's giving you where it is on your SCREEN not where it is in GA$$anonymous$$E SPAC$$anonymous$$

avatar image $$anonymous$$ · Jul 18, 2013 at 07:28 AM 0
Share

this line hit.point.z = 0f; throwes me error CS1612: Cannot modify a value type return value of `UnityEngine.RaycastHit.point'. Consider storing the value in a temporary variable

avatar image Narv · Jul 18, 2013 at 09:58 AM 0
Share

I guess I forgot that it's read only. So

Vector3 newpoint = hit.point; newpoint.y = 0;

Where you used hit.point, use newpoint ins$$anonymous$$d.

Show more comments

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

15 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Camera Script - RtS style, rotation issues 0 Answers

Rotating object with a mouse movement 0 Answers

How to rotate Character on Y axis along with the mouse rotation? 0 Answers

Shooting problem the bullets fly in diffrent direction then player looks? 2 Answers


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