- Home /
The question is answered, right answer was accepted
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!!
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.
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!
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.
thanks for answering. i will try it out. I am unsure how to use $$anonymous$$athf.Clamp in Rotation though
thanks
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);
}
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);
}
}
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.
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.
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 ;)
That was the code. it does the same thing the velocity did
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.
Follow this Question
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