- Home /
First rpg how to shoot arrows ?
Hi there. I am new to unity, so i really need help with start :/ My question is :
i have a character with bow and arrow's. I have animation when he shoots, but how to make the bow fly in direction ? It has to collide with object's it hits. What should be direction ? I would really like to make a croshair in front of my character, and every script, tutorial i have seen... it's not good enough.
Should i be working on javascript for arrow ? or make a C# file ?
Doesn't matter which language you use. Just use the one you're comfortable with.
If you search this site for 'arrow' you'll find a lot of questions very similar to this.
Short version - instantiate the arrow at the bow's position with the bow's rotation, give it a velocity, when it hits something, disable its movement.
thanks for reply, but as i already told.. nothing helpfull is out there. i need help from point 0. I just found that a rigidbody should be attached to arrows joint, not the arrows its self ? am i right ?
Answer by subcod · Aug 13, 2012 at 06:22 PM
USE PHYSICS.RAYCASTHIT! SEARCH IT UP IN UNITY REFERENCES!
Answer by GlitchBait · Aug 13, 2012 at 06:29 PM
Start with basic objects and play with instantiate to achieve what exactly you're looking for.
Here is the basic Java code for instantiating an object from the object that holds this script.
Most/all of the term other users will suggest to you as solutions will be located in the Unity Script Reference. A reference, lexicon and bible. http://docs.unity3d.com/Documentation/ScriptReference/index.html
Good luck and best wishes! // Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
function Update () { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}
Answer by subcod · Aug 13, 2012 at 06:27 PM
//using javascript var Crosshair:Texture; var bullet:Transform; var mat:Material; function crossharRed()//This tells you if the bullit will hit or not
{
if(Physics.Raycast(bullet.gameObject.transform.position,Vector3.forward, distanceOfShot))
{
mat.mainTexture= Crosshair;
} }
function fire()
{
Instantiate(object.gameObject,object.gameObject.position,object.rotation) }
function Update()
{
object.gameObject.transform.position.x+=50; }
This is a basic code for what you are doing. I suggest you take a look at the Unity3d script reference if you have any questions or to trouble shoot any mistakes in the code. http://docs.unity3d.com/Documentation/ScriptReference/MeshRenderer.html
Answer by subcod · Aug 13, 2012 at 06:23 PM
//using javascript var Crosshair:Texture; var bullet:Transform; var mat:Material; function crossharRed()//This tells you if the bullit will hit or not
{
if(Physics.Raycast(bullet.gameObject.transform.position,Vector3.forward, distanceOfShot))
{
mat.mainTexture= Crosshair;
} }
function fire()
{
Instantiate(object.gameObject,object.gameObject.position,object.rotation) }
function Update()
{
object.gameObject.transform.position.x+=50; }
This is a basic code for what you are doing. I suggest you take a look at the Unity3d script reference if you have any questions or to trouble shoot any mistakes in the code. http://docs.unity3d.com/Documentation/ScriptReference/MeshRenderer.html
Answer by tw1st3d · Aug 13, 2012 at 07:49 PM
function Update() {
if(Input.GetButtonDown("Fire1")) {
arrow.useGravity = false;
var instance : Rigidbody = Instantiate(arrow, muzzlePoint.position, Quaternion.identity);
instance.velocity = muzzlePoint.forward * 100;
}
}
You're accessing arrow before it's set, you're disabling gravity on it (?), you're not giving it a meaningful rotation, and you've got 'muzzle' stuff going on. Not that varnames do anything to logic but it's best not to confuse the programmer.
I'm sure if he's developing an RPG, he'd have a variable set for arrow, and I wouldn't be required to do so for him. Therefore, setting the gravity for a variable that he should already have would cause no problem.
Whoops, saw 'arrow' and assumed that would be the instantiated object. $$anonymous$$y dumb.
Your answer
Follow this Question
Related Questions
Hard question about 2d sprites in 3d world 2 Answers
thrid person help 1 Answer
How to go about making a huge game world 2 Answers
How to destroy a certain object with another certain object? 1 Answer
c# Unexpected symbol 'foreach' 1 Answer