I’m from turkey and I can’t speak english.I am do a 2.5d game but I Can’t rotate a gun.This my script and error:
using UnityEngine;
using System.Collections;
public class silahhareket : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.42f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
error:
NullReferenceException: Object reference not set to an instance of an object
silahhareket.Update () (at Assets/silah ayarları/silahhareket.cs:17)
and I have a different scripts it’s a fire scripts But not a cube fire because I can’t find the cube fire gun(if you know a cube fire gun please write the scripts):
using UnityEngine;
using System.Collections;
public class fire : MonoBehaviour {
public float fireRate = 0;
public float damage = 10;
public LayerMask notToHit;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake () {
firePoint = transform.FindChild ("FirePoint");
if (firePoint == null) {
Debug.LogWarning ("No firePoint? What?!");
}
}
// Update is called once per frame
void Update () {
if (fireRate == 0) {
if (Input.GetButtonDown ("Fire1")) {
shoot();
}
} else {
if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
timeToFire = Time.time + 1/fireRate;
shoot();
}
}
}
void shoot () {
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, notToHit);
Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
if (hit.collider != null) {
Debug.Log ("test");
Debug.DrawRay (firePointPosition, hit.point, Color.red);
}
}
}
error:
NullReferenceException: Object reference not set to an instance of an object
fire.shoot () (at Assets/silah ayarları/fire.cs:35)
fire.Update () (at Assets/silah ayarları/fire.cs:25)
(Sorry I can some speak english)