If anyone remembers MicroBattles on IOS, I'm trying to make a version on the PC of the space game. The ships rotate and the player presses the space bar to propel the ship. The direction in which it is propelled is based on the rotation of the ship when the button is pressed (the ship is constantly rotating until the player chooses to thrust, then the ship stops rotating and goes in whatever direction the front was facing). How would I get this into C-Sharp code? I'm not entirely sure how I would get the direction travel to be based on the rotation, I'm very new to coding and the game design teacher apparently doesn't know code either (I don't know why he's teaching?). Thanks.
2D Rigidbody The current code: public float speed; public bool spinning = true; private Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); rb.AddForce(movement * speed); if (Input.GetKey(KeyCode.Space)) { spinning = false; } if (spinning == true) { transform.Rotate(new Vector3(0, 0, -30) * Time.deltaTime); } } }
Add rigidbody to your object and this should work... Code (CSharp): public class RotateAndMove : MonoBehaviour { public float rotateSpeed = 100; // desired rotation speed private float currentRotateSpeed; // current rotation speed public float moveSpeed = 100; // move speed // Update is called once per frame void Update() { // check for movement, if moving, don't rotate if ( gameObject.GetComponent<Rigidbody2D>().velocity.magnitude > 0.2f ) // or 0.0f :). Just check for zero movement.... { currentRotateSpeed = 0; } else { currentRotateSpeed = rotateSpeed; } transform.Rotate( new Vector3( 0, 0, 1 ) * Time.deltaTime * currentRotateSpeed ); // rotate on z axis if ( Input.GetKeyDown( KeyCode.A ) ) { gameObject.GetComponent<Rigidbody2D>().AddRelativeForce( new Vector2( 0, moveSpeed ) ); // add force on objects y axis } } }
Also, another question: I've added four colliders/triggers around the screen, which will make the ship transform its position when it collides with one of them. I need to have the ship get transformed to the opposite position across from the edge (i.e. if the ship hits the bottom collider, it will come back from the top at the same position). The current code I have: using UnityEngine; using System.Collections; public class DeathRespawn : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Transform teleport; void OnTriggerEnter(Collider col) { player.transform.position = teleport.transform.position; } } Thanks
So, what's the question? You should have way to detect which collider you hit ( use tags ) and, based on that information, send object to teleport location...
Does other collider exists where teleport target is? Is it possible that you, actually, teleport your object to other collider which teleports it back and so on? Anyway, other solution might be to check where your object is. If it's beyond screen edge ( pay attention on world/screen coordinates ! ) just send it to opposite side. Something like this, this will check object moving on left and chek if it's beyond left screen edge ( leftScreenlimit ). Note - it's in World space. Code (CSharp): void CheckBorders() { if ( gameObject.transform.position.x < leftScreenLimit ) // am I beyond defined border on X? { Vector2 oldPosition = gameObject.transform.position; // my old position gameObject.transform.position = new Vector2( oldPosition.x * -1, oldPosition.y ); // multiply X by -1 to send it on other side, Y remains same } } This is code is just to show you basic idea...
The code definitely works, the only question I have is what object I should put the script on? The actual player ship or an empty game object? Also, where's the best place to actually learn C-Sharp coding for this kind of stuff? My class has not really taught it.
I've gotten this code, but it doesn't seem to be working, the ship just keeps going. Can you see anything wrong with it?
@besomuk using UnityEngine; using System.Collections; public class Edges : MonoBehaviour { private const double V = 0.6; int leftScreenLimit = 17; int rightScreenLimit = 1; double topScreenLimit = V; double bottomScreenLimit = -V; public GameObject player; private void OnTriggerEnter2D(Collider2D collision) { { if (player.transform.position.x < leftScreenLimit) // am I beyond defined border on X? { Vector2 oldPosition = gameObject.transform.position; // my old position player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same } if (player.transform.position.x < rightScreenLimit) // am I beyond defined border on X? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same } if (player.transform.position.y < topScreenLimit) // am I beyond defined border on Y? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same } if (player.transform.position.y < bottomScreenLimit) // am I beyond defined border on Y? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same } } } }
First, pay attention on your oldPosition variable. It's holding position of the object that has your script attached - not player object. oldPosition should take player position NOT gameObject position. Second, try with if ( player.transform.position.x > rightScreenLimit ), not if ( player.transform.position.x < rightScreenLimit )
@besomuk I've added some rectangles around the sides and made them 2D triggers, so that the code will start when the ship hits it. Unfortunately, the ship just disappears after.
using UnityEngine; using System.Collections; public class Edges : MonoBehaviour { private const double V = 0.6; int leftScreenLimit = 17; int rightScreenLimit = 1; double topScreenLimit = V; double bottomScreenLimit = -V; public GameObject player; private void Update () { { if (player.transform.position.x < leftScreenLimit) // am I beyond defined border on X? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same } if (player.transform.position.x > rightScreenLimit) // am I beyond defined border on X? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.x * -1, oldPosition.y); // multiply X by -1 to send it on other side, Y remains same } if (player.transform.position.y < topScreenLimit) // am I beyond defined border on Y? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same } if (player.transform.position.y < bottomScreenLimit) // am I beyond defined border on Y? { Vector2 oldPosition = player.transform.position; // my old position player.transform.position = new Vector2(oldPosition.y * -1, oldPosition.x); // multiply Y by -1 to send it on other side, X remains same } } } }