- Home /
[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);
}
}
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).
This works, but they are following mouse, i just need to move in mouse direction, and its 2d game.
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).
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$$
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
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.
Your answer
Follow this Question
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