• 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 t$$anonymous$$s? I tried t$$anonymous$$s, but it doesnt seem to work. Thanks for any help!

public class Bullet : MonoBehaviour {

 public float speed = 50f;
 
 // Use t$$anonymous$$s 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 $$anonymous$$ts, so you will use a Ray and a RaycastHit object for t$$anonymous$$s. 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 $$anonymous$$t;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out $$anonymous$$t))
             {
                transform.position = Vector3.MoveTowards(transform.position, $$anonymous$$t.point, Time.deltaTim * speed);
             }
     
     }
 }


T$$anonymous$$s moves somet$$anonymous$$ng 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 watc$$anonymous$$ng the video, if that isn't the destired effect you can get the $$anonymous$$t.point value in Start() and then move towards $$anonymous$$t.point every Update() function. (assuming t$$anonymous$$s 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 (assuming 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 GAME SPACE.

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 instead.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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