Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed May 07, 2016 at 10:20 PM by mnmwert for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by mnmwert · May 02, 2016 at 05:17 AM · rotationscripting problemdirectionbulletgun

making bullet go travel at angle fired and limit gun rotation in Unity C# version 5.2 2D

I have two scripts.

rotate gun:

 using UnityEngine;
 using System.Collections;
 
 public class rotateGun : MonoBehaviour
 {
     public float rotateSpeed;
     public float  minRotate, maxRotate;
     public float noRotation;

     void start() { }

     void Update()
     {

         if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(0.0f, 0.0f, -rotateSpeed);
         }
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(0.0f, 0.0f, rotateSpeed);
         }
     }
 }

and fireGun:

 using UnityEngine;
 using System.Collections;
 
 public class FireGun : MonoBehaviour
 {
     public GameObject shot;
     public Transform shotSpawn;
     public float fireRate;
     private float nextFire;
     public float fireSpeed;
     private GameObject cloneShot;
     public Transform arm;
     public float Direction = 0;

     void Start ()   {  }
 
     void Update ()
     {
        if(Input.GetKey(KeyCode.Space) && Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
             cloneShot = (GameObject)Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
             cloneShot.GetComponent<Rigidbody>().velocity = new Vector3(fireSpeed,Direction);
         }
         if (arm.transform.rotation.z > 0 && arm.transform.rotation.z < 90)
         {
             Direction += 1;
         }
         if (arm.transform.rotation.z < 0 && arm.transform.rotation.z >350)
         {
             Direction -= 1;
         }
     }
 }

in rotateGun i need to limit the rotation of the z axis in side view (90 & 350)

in fireGun i need to fire the bullet in the degree the gun's rotation on the z axis.

I have tried for weeks to fix these and have not found anything.

Answers are appreciated thoroughly!

Thanks!!

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

  • Sort: 
avatar image
1

Answer by Nido · May 02, 2016 at 02:34 PM

If you want to clamp a value between x and y, you have to use Mathf.Clamp,

http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

Using this, the clamped value will never be less than the minValue (90f), neither more than the maxValue (350f).

And for the firing direction, I had not tested but I think you could do this.... (line 13 in your code)

 cloneShot.GetComponent<Rigidbody>().velocity = arm.transform.forward;

... since forward is the z vector.

Comment
Add comment · Show 16 · 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 mnmwert · May 02, 2016 at 10:09 PM 1
Share

Thank you for answering!

in your first answer when you suggested the use of $$anonymous$$athf.Clamp i tried that but it did not work. I asked my self why and I found out that you gave me the answer to solving the position. I am trying to limit the ROTATION of the object (on the z angle 90 traveling up and 350 traveling down).To make it a bit more clear and leave no room for doubt; I have a player with and invisible arm i am trying to rotate.I have made it rotate but i need to stop its rotation when it hits the angle 90 and 350 on the z axis (This is in side view in 2D so that would be up/down ) . Thanks for answering that and i will change my player boundary to suit that in the future.

As for the firing direction you almost got it right. It will go in the direction i want it to but the bullet does not have any force behind it.

Thank you so much for trying and i hope you can answer this problem!

avatar image Nido mnmwert · May 03, 2016 at 12:38 PM 1
Share

Well, if I'm getting the bullet problem right, you should use Rigidoby.AddForce ins$$anonymous$$d of velocity. This will provide an initial speed giving the direction(arm.transform.forward) as first parameter and the best Force$$anonymous$$ode for your purpose (impulse, I assume). Hope this helps you to reach the right effect! ;)

PS: If you need few lines to apply the $$anonymous$$athf.Clamp, tell me. With this method you can clamp any float value you want.

avatar image mnmwert · May 03, 2016 at 12:51 PM 0
Share

thanks for answering. i will try it out. I am unsure how to use $$anonymous$$athf.Clamp in Rotation though

thanks

avatar image Piflik mnmwert · May 03, 2016 at 01:13 PM 0
Share
 float _angle;
 public float rotateSpeed;
 
 void Update() {
     _angle += Input.GetAxisRaw("Horizontal") * rotateSpeed * Time.deltaTime;
     _angle = $$anonymous$$athf.Clamp(_angle, 90, 350);
 
     transform.rotation = Quaternion.AngleAxis(_angle, transform.forward);
 }

avatar image Nido mnmwert · May 03, 2016 at 01:17 PM 1
Share

Put this in a simple cube in a scene for see how it works. This is the clamping of the Z value of the rotation.

using UnityEngine; using System.Collections;

 public class CubeBeh : $$anonymous$$onoBehaviour 
 {
     public float rotateSpeed = 5f;
 
     private void Update()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
         {
             transform.Rotate(0.0f, 0.0f, -rotateSpeed);
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
         {
             transform.Rotate(0.0f, 0.0f, rotateSpeed);
         }
         float newZ = transform.eulerAngles.z;
 
         newZ = $$anonymous$$athf.Clamp(newZ, 90f, 350f);
 
         transform.eulerAngles = new Vector3(0, 0, newZ);
     }
 }
avatar image mnmwert · May 03, 2016 at 04:08 PM 0
Share

the rotating works but i had to change 350 to 0 and reverse 90,0 to 0,90.

I'll get back to the firing.

avatar image mnmwert · May 03, 2016 at 04:17 PM 0
Share

AddForce just does the same thing velocity did;

here is my line of code:

cloneShot.GetComponent().AddForce(arm.transform.forward,Force$$anonymous$$ode.Impulse);

I may have miss read what you wanted me to do.

avatar image Nido mnmwert · May 03, 2016 at 04:26 PM 0
Share
 cloneShot.GetComponent<Rigidbody>().AddForce(arm.transform.forward,Force$$anonymous$$ode.Impulse);

If you copy-pasted that line, be careful with that "Rigidbody"! This line should work fine. But until tomorrow I won't be at pc. I'll take a look then ;)

avatar image mnmwert Nido · May 03, 2016 at 04:28 PM 0
Share

That was the code. it does the same thing the velocity did

Show more comments
avatar image mnmwert · May 05, 2016 at 12:34 PM 0
Share

it does not have any force pushing it forward. it has some when it is tilted up but thas none when zero. this is because in the script has

arm.transform.forward

as the main force produced. it doesn't have any other force behind it to push it.

Show more comments

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I keep my character facing the direction of travel after movement stops? 1 Answer

Shoot an object and have it move based on rotation 1 Answer

Can't make a bullet move in the direction the actor is facing 2 Answers

Help with gun accuracy in degrees. 3 Answers

Problem with Shooting Accuracy 0 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