Character Rotation when firing a gun

Hi guys, I’m having some trouble with my movement script ( I think ). So here is what I want to do -

-When moving with WASD I want my char to face the direction that I’m moving, because I have attached a barrel from which the bullets come out. Problem is, I can’t fire the bullets in direction. This is the script attached to the barrel which is child of the Player -

using UnityEngine;
using System.Collections;

public class UsingInstantiate : MonoBehaviour
{
	public Rigidbody rocketPrefab;
	public Transform barrelEnd;
	public int speed;

	void Update ()
	{

		if(Input.GetButtonDown("Fire1"))
		{
			Rigidbody rocketInstance;
			rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
			rocketInstance.AddForce(barrelEnd.forward * speed);
		}
	}
}

How to do this? I’ve tried many movement or rotation scripts but I can’t seem to fire in the direction that I’m moving and when I move to the left and stop I still want to shoot to the left without pressing any keys… I am new to Unity so I can’t figure it out by myself, I’ve searched and read but nothing :frowning: PLEASE HELP !

I think you need to set your players y rotation on each movement input.

So, if A direction, y rotation = 90, W = 0 and so on.

ug, a typical response that makes no sense and offers no solution… 4.5k posts you would think something decent might actually come out…

rocketInstance.AddForce(barrelEnd.forward * speed);

you could try changing to…

rocketInstance.AddForce(rocketInstance.forward * speed);

although they are probably the same value, so what I would recommend is putting this code into your update.

Debug.DrawLine(barrelEnd.position, barrelEnd.position + barrelEnd.forward * 10, Color.green, 2, false);

See if it points in the correct direction, let me know the result and ill see if I can help further.

You are absolutely correct.
I am yet to make a worth while post.
Thank you Obi Wan.

Thats Mr Obi Wan Kernobi to you! :stuck_out_tongue:

Remember you’re dealing with people who wouldnt know what “y rotation = 90, W = 0” means.

The docs used to say that you should never mess with those values unless you know quaternions inside out, but now it simply says "You almost never access or modify individual Quaternion components (x,y,z,w); "

James,
W, in my case, represents the W in ASWD . Nothing to do with Quaternion’s.
Duhhhhh.

oh right. LOL.

as you were then.

R2D2
~Makes R2D2 noise.

Thanks James! But what I actually want to do is to rotate my char, my barrel is infron of my char like this -

And this is what my movement code currently looks like -

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	
	public float speed;
	
	void Start ()
	{
		renderer.material.color = Color.magenta;
	}
	
	void Update ()
	{
		transform.eulerAngles = new Vector3(0,0,0);
		if (Input.GetKey (KeyCode.UpArrow)) {
			transform.Translate(Vector3.forward*(speed*Time.deltaTime));
		}
		if (Input.GetKey (KeyCode.LeftArrow)) {
			transform.Translate(Vector3.left*(speed*Time.deltaTime));
		}
		if (Input.GetKey (KeyCode.DownArrow)) {
			transform.Translate(Vector3.back*(speed*Time.deltaTime));
		}
		if (Input.GetKey (KeyCode.RightArrow)) {
			transform.Translate(Vector3.right*(speed*Time.deltaTime));
		}
		
	}
	
}

What I want is to rotate my char when I’m going to the left he turns left and the endBarrel as well so that I can shoot to the left. Then the same thing with each side! I’m trying to make something like SHOOTER for 2 players on one keyboard, the camera is on top so they both see what they are doing but I can’t seem to execute this thing. The movement code works fine, until the shooting needs to happen … :frowning: