- Home /
Bullet facing
Hey! I want to make my bullets that exist revolve around a place when it is clicked but the the bullets always revole around the same place. I have this code in the spawner to give directions:
using UnityEngine;
public class FireScript : MonoBehaviour
{
public float hitx = 0;
public float hity = 0;
public float hitz = 0;
public Vector3 hit1 = new Vector3 (0, 0, 0);
public GameObject Bullet;
public Vector3 hit3;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
GameObject bullet = Instantiate (Bullet, transform.position, Quaternion.identity) as GameObject;
if (hit.collider.name == "S_Plane") {
hit1 = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
BulletShootScript bulletScript = bullet.GetComponent<BulletShootScript> ();
bulletScript.hitx2 = hit.point.x;
bulletScript.hity2 = hit.point.y;
bulletScript.hitz2 = hit.point.z;
}
}
}
}
}
And this code in the bullets:
using UnityEngine;
using System.Collections;
public class BulletShootScript : MonoBehaviour {
Vector3 ShootPos = new Vector3(0, 0, 0);
public float hitx2 = 0;
public float hity2 = 0;
public float hitz2 = 0;
public Rigidbody rb;
public FireScript fs;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
fs = GetComponent<FireScript>();
rb.AddForce( transform.forward * 100 );
}
// Update is called once per frame
void Update () {
Vector3 hit2 = new Vector3 (hitx2, hity2, hitz2);
Quaternion rotation = Quaternion.LookRotation(hit2 - transform.position);
transform.rotation = rotation;
rb.AddForce( transform.forward * 100 );
}
}
Can somebody please help me?
Comment
Your answer
Follow this Question
Related Questions
How to assign x and y transform values of an gameobject to user defined variables? 1 Answer
Why can't i use these arrays to access positions and objects properties? Please help! 3 Answers
How to set the position of an object using variables. 1 Answer
C# Using Variables for GUI Rectangles 0 Answers
Best Unet. 0 Answers