Rotating & fixing the position of an object to one spot

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);
	}
}

}

So you have a transform holding all the shelves your AIs visit. What's the other method doing:? There isn't much to go on with it. With a Serialized collection, you can actually assign the points (transforms) by dragging the object with that transform from the Hierarchy to the Inspector, so you wouldn't actually need a loop in that case.

1 Answer

1

Alright. I’ll tackle this one part at a time!

Camera: If you want your player to stay in the middle of the screen, you’ll have toi make the camera follow the player. You can do this by making a camera controller script, and setting the cameras transform to that of the player, plus the initial offset.

Vectror3 offset = transform-player.transform;
transform = player.transform+offset;

Next you can make your ship stop turning by changing the getkey to getaxis. This removes the need for an if statement, and is generally more preferred than getkey. You can have more than 2 axis, and can set them yourself in unity by going to Edit > Project Settings > Input.

Update()
{
    transform.Rotate (Vector3.up, Input.GetAxis("Rotational")*turnSpeed * Time.deltaTime);
}

I see what you mean...

I'm referring to your InitializeWaypoints method. In the inspector, on the component holding waypointTransforms, you can set the size of the collection, and drag Transforms from the Scene view into this collection. This essentially replaces the InitializeWaypoints method. Are you pulling data for these transforms from some storage, or generating the waypoints dynamically somehow?