C# Don't just want to shoot directly forwards

        void Update() {
        if(Input.GetMouseButtonDown (1))
            {
            Shoot();
            }
        }

        void Shoot(){
            {
            Rigidbody magic;
            magic = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            magic.velocity = transform.TransformDirection(Vector3.forward * 5);
            }
        }
}

I have got most of my shoot script to work how I would like, however I have not set up shooting upwards or downwards. I can only shoot directly in front of the character being controlled. Is there an easy way to consider the Y when shooting to add to the script above?

Would it be as simple of changing or adding to Vector3.forward?

You could create a new Vector3 before you instantiate the object. This vector3 could then be set to be the same as transform.rotation except for where you want it to be different, such as making the x value be transform.rotation.x + 90 to be rotated by 90 on the x axis. Then, on line 12 where you instantiate the magic object, instead of saying transform.rotation you could use the new Vector3 as your rotation. From there, wherever you make it face it will move in that direction because you move it with Vector3.forward.

Do you want me to show you some code or do you want to try it yourself before I spoil the puzzle:)?

Thanks Diericx, I am not entirely sure how to make the changes you’re suggesting. Could you show me the code? :slight_smile:

Absolutely! So here is how you would have one shoot in the original direction and one rotated 90 degrees on the X axis. I’m not actually sure where that will shoot but you’ll see the difference.

//This code will create 2 magic objects that move in 2 different directions MAYBE
//It might be that this just spins it. Try adding 90 to the y or z axiz instead of x if
//this doesn't immediately work.
void Shoot(){
{
       //---this is the original magic object---
       Rigidbody magic;
       magic = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
       magic.velocity = transform.TransformDirection(Vector3.forward * 5);
    

       //---here will be the rotated one---
       Rigidbody magic2;
       //here we create the new vector3 and use transform.rotation and edit the x value
       Vector3 newRotation = new Vector3(transform.rotation.x + 90, transform.rotation.y, transform.rotation.z);
       //now we use that rotation instead of transform.rotation
       magic2 = Instantiate(projectile, transform.position, newRotation) as Rigidbody;
       magic2.velocity = transform.TransformDirection(Vector3.forward * 5);
}
1 Like

Thanks Diericx the code looks really good, however I’m not sure why but now errors are occurring. “Argument #3' cannot convert UnityEngine.Vector3’ expression to type UnityEngine.Quaternion'*" and "*the best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)’ has some invalid arguments”. I’ll have a play with on Monodevelop and see if I can find a solution.

The error is coming from line 16. The new rotation is being declared and used as a Vector3, however Instantiate uses a Quaternion and not a Vector3. This should fix the error, just replace that line with this.

Quaternion newRotation = new Quaternion(transform.rotation.x+90, transform.rotation.y, transform.rotation.z, 0f);
1 Like

Thank you both. This has been really helpful.

Ahhh I always do this! Thanks for correcting me:)

(Just took a break from reading The Fellowship of the Ring to comment and your quote is very coincidentally relevant :smile: )

I’m glad!:smile:

1 Like