- Home /
Change bullet direction with player direction
I have been reading for ages about this and each question I find suggests what I aready have but not sure why my code is not working.
using UnityEngine;
using System.Collections;
public class shoot : MonoBehaviour {
public GameObject bullet;
//Forces to apply to our projectile
public float projectileForwardForce;
public float projectileUpForce;
// Update is called once per frame
void Update () {
//If our left mouse button is clicked
if (Input.GetMouseButtonDown(0))
{
Vector3 offsetForward = transform.forward * 2;
Vector3 offsetUp = transform.up;
//Calculate our offset position
Vector3 finalPosition = transform.position + offsetForward + offsetUp;
//Creates a new object at our current position and rotation
GameObject myObject = Instantiate(bullet, finalPosition,bullet.transform.rotation) as GameObject;
//Apply a force to our tank shell to make it fly into the distance
myObject.rigidbody.AddRelativeForce(myObject.transform.forward * projectileForwardForce, ForceMode.Impulse);
//Add a small force upwards to make it fly into the arc a little
myObject.rigidbody.AddRelativeForce(myObject.transform.up * projectileUpForce, ForceMode.Impulse);
Destroy(bullet,2.0f);
}
}
}
Answer by robertbu · May 11, 2014 at 01:58 AM
You have some problems here. First, you are setting the bullet's rotation to the rotation of the prefab, not the rotation of the player. Assuming this script is on the player, you need to change line 27:
GameObject myObject = Instantiate(bullet, finalPosition, transform.rotation) as GameObject;
Your other problem is that you are using world vectors to apply force in the local coordinate system. Try this instead:
Vector3 vForce = transform.forward * projectileForwardForce + transform.up * projectileUpForce;
myObject.rigidbody.AddForce(vForce, ForceMode.Impulse);
Since the rotation of the player object and the rotation of the projectile match, we an use the player's 'forward' and 'up'.
Cool, that worked to get the bullet moving in the direction the player is facing, but the bullet itself has not rotated. I took out the lines
//Apply a force to our tank shell to make it fly into the distance
myObject.rigidbody.AddRelativeForce(myObject.transform.forward * projectileForwardForce, Force$$anonymous$$ode.Impulse);
//Add a small force upwards to make it fly into the arc a little
myObject.rigidbody.AddRelativeForce(myObject.transform.up * projectileUpForce, Force$$anonymous$$ode.Impulse);
And put in the code you suggested
Vector3 vForce = transform.forward projectileForwardForce + transform.up projectileUpForce; myObject.rigidbody.AddForce(vForce, Force$$anonymous$$ode.Impulse); Was this what you meant or is the bullet not rotating because I implemented the code incorrectly?
Assu$$anonymous$$g you've built your projectile so that the front of the projectile faces positive 'z' when the rotation is (0,0,0), then you can add this line:
myObject.transform.rotation = Quaternion.LookRotation(vForce);
If you have gravity enabled, then you probably want to scratch the previous line, and add a script to the projectile that has this line:
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
This will keep the front of the projectile point in the direction of movement as it arcs to its destination.
I used myObject.transform.rotation = Quaternion.LookRotation(vForce); I have gravity enabled and this worked although the bullet is shooting out vertical ins$$anonymous$$d of horizontal. The other was making the player tilt.
The second line will only make the player tilt if you put it in the same script. Quoting from my comment:
add a script to the projectile
The script with this line needs to be on the projectile, not in the shooting code. This line will change the projectile's rotation as it curves through the scene.
As for the projectile being vertical, you've authored your mesh wrong. You need the forward of the projectile to face positive 'z' when the rotation is (0,0,0).
Yep, all good, I had the shape of the bullet having the length going in the y direction and not in the z direction as you said. All good now. Than you very much for your help!
Your answer
Follow this Question
Related Questions
Shoot an object and have it move based on rotation 1 Answer
Bullets Based on Orientation 1 Answer
How to make a gun shoot? 2 Answers
Shoot with ray cast angle 1 Answer
Problem with Shooting Accuracy 0 Answers