- Home /
Firing arrow in direction depending on Camera
Hi there. I wonder how to make this script work.
if(Input.GetMouseButton(1)) { //arrow.useGravity = false;
vec = new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.MainCamera.transform.position.z+1);
var instance :Rigidbody = Instantiate(arrow, muzzlePoint.position, muzzlePoint.rotation);
instance.transform.Translate(vec);
}
I don't get what is the problem. I created Vector3 which has x,y and Z coordinates...
x will be ( left or right) y will be ( hight ) z will be ( distance )
Anyways, if someone has better idea of firing arrow's pls copy paste link/script advice.
Thanks for any help in advance.
Answer by fafase · Aug 15, 2012 at 05:26 AM
I guess you could try
var other:Camera;
function Start(){
other = FindObjectOfType(Camera);
}
function Update () {
if(Input.GetMouseButtonDown(1)){
var hit : RaycastHit;
if(Physics.Raycast(other.ScreenPointToRay(Input.mousePosition), hit)){
var vec:Vector3 = hit.point;
}
var instance :Rigidbody = Instantiate(arrow, muzzlePoint.position, muzzlePoint.rotation);
instance.Movement(vec);
//instance.rigidbody.AddForce(muzzlePoint.transform.forward*forceApplied);
// I 'd rather use the rigidbody, looks more natural.
}
In a arrow script
var vect:Vector3;
function Update(){
transform.Translate(vect*Time.deltaTime);
}
function Movement(v:Vector3){
vect = v;
}
You use the ScreenPointToRay with the click of the position of the mouse on the screen to convert to the world. In the muzzle script you call the function from the arrow that sets the vector for the translation. As I showed, you would rather go for a rigidbody that handles physics more easily for you. On top of that your arrow will have a natural parabolic path.
Answer by Neroner23 · Aug 15, 2012 at 12:40 PM
Thanks fafase, this looks really nice. i tryed adding force to my arrow to fire it from my muzzle point, but when use .forward it goes left, and when i use .down it goes forward. What do u think why ??
For info this should be a comment under my answer. Now your issue, when you select the muzzle object you should have the three red green and red arrows indicating the transform. The blue one indicates forward. If your object has a rotation then that could be the reason why it goes left.
Your answer
Follow this Question
Related Questions
How to destroy an object - destroy problems 3 Answers
How to translate an object correctly? 1 Answer
RigidBody clips into corners 3 Answers
why my wander script makes my enemies move badly? 1 Answer
how do you exit GUI 2 Answers