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 PLEASE HELP !
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); "
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 …