So I’ve been trying to fix my object to the middle of screen and rotate it with the left and right arrows. I’ve managed to get it to rotate after some research but it’s just constantly rotating.
I’m trying to make the object rotate while being able to still shoot. I’ve been stuck on this code for a bit now and can’t seem to fix it to a position and get it to rotate without getting an error/nothing happening/wrong thing happening.
Here’s the code I’ve been working with:
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public static int lives = 3;
public float speed = 1f;
public float turnSpeed = 50f;
public float horMin = -6f;
public float horMax = 6f;
public float vertMin = -4f;
public float vertMax = 4f;
public GameObject projectile;
public Transform socketProjectile;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float transV = Input.GetAxis ("Vertical") * speed * Time.deltaTime;
float transH = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
transform.Translate (transH, transV, 0);
Vector3 pos = transform.position;
pos.x = Mathf.Clamp (pos.x, horMin, horMax);
pos.y = Mathf.Clamp (pos.y, vertMin, vertMax);
transform.position = pos;
if (Input.GetKey (KeyCode.LeftArrow))
transform.Rotate (Vector3.up, -turnSpeed * Time.deltaTime);
transform.Rotate (Vector3.up, turnSpeed * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.Space)) {
Instantiate (projectile, socketProjectile.position, socketProjectile.rotation);
}
}
}