Need help with Vehicle Movement, Collisions, Firing.
I'm pretty much new to Unity and still having trouble just doing the most basic of functions inside the programme. I need to make a Tower Defense game. I have made a tank and a building and I want the player to control the tank and fire upon enemies.
As it stands the tank stutters upon movement, the two turrets fire simultaneously instead of one or the other, the tank drives straight through the building or hits it and floats up backwards.
I have tried various edits in the inspector such as Rigidbody for both the Tank and Building, Rigidbody for one or the other, box colliders, turning on / off gravity, adjusting the mass.
There is a link with quick gameplay.
https://photos.google.com/search/_tra_/photo/AF1QipM1p3sn3_ZtsTgI1fMVYHfsYALrFya_9Lipa8ZG Here are my scripts:
public class Turret : MonoBehaviour {
void Update()
{
// Rotates the turret left when the A ARROW is pressed
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -0.1f, 0);
}
// Rotates the turret right when the D ARROW is pressed
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 0.1f, 0);
}
}
{ public GameObject MyScript;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "building")
{
MyScript.GetComponent<Collisions>().enabled = true;
}
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "building")
{
MyScript.GetComponent<Collisions>().enabled = false;
}
}
}
public GameObject projectilePrefab;
public Transform projectileSpawn;
public float projectileSpeed = 30f;
public float lifeTime = 3f;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
FireL();
}
if (Input.GetKeyDown(KeyCode.S))
{
FireR();
}
}
private void FireL()
{
GameObject CylinderL = Instantiate(projectilePrefab);
Physics.IgnoreCollision(CylinderL.GetComponent<Collider>(), projectileSpawn.parent.GetComponent<Collider>());
CylinderL.transform.position = projectileSpawn.position;
Vector3 rotation = CylinderL.transform.rotation.eulerAngles;
CylinderL.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
CylinderL.GetComponent<Rigidbody>().AddForce(projectileSpawn.forward * projectileSpeed, ForceMode.Impulse);
StartCoroutine("DestroyProjectile");
}
private void FireR()
{
GameObject CylinderR = Instantiate(projectilePrefab);
Physics.IgnoreCollision(CylinderR.GetComponent<Collider>(), projectileSpawn.parent.GetComponent<Collider>());
CylinderR.transform.position = projectileSpawn.position;
Vector3 rotation = CylinderR.transform.rotation.eulerAngles;
CylinderR.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
CylinderR.GetComponent<Rigidbody>().AddForce(projectileSpawn.forward * projectileSpeed, ForceMode.Impulse);
StartCoroutine("DestroyProjectile");
}
private IEnumerator DestroyProjectile(GameObject projectile, float delay)
{
yield return new WaitForSeconds(delay);
Destroy(projectile);
}
} public class TankMovement : MonoBehaviour {
Rigidbody m_Rigidbody;
public float m_Speed;
private bool isGrounded;
void Start()
{
//Fetch the Rigidbody component you attach from your GameObject
m_Rigidbody = GetComponent<Rigidbody>();
//Set the speed of the GameObject
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
{
//Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
m_Rigidbody.velocity = transform.forward * m_Speed;
}
if (Input.GetKey(KeyCode.DownArrow))
{
//Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view)
m_Rigidbody.velocity = -transform.forward * m_Speed;
}
if (Input.GetKey(KeyCode.RightArrow))
{
//Rotate the sprite about the Y axis in the positive direction
transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * m_Speed, Space.World);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
//Rotate the sprite about the Y axis in the negative direction
transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * m_Speed, Space.World);
}
}
}
These are some photo links with object properties. I realise the mass differences are all over the place and various other properties, but I have been adjusting everything over and over again.
https://photos.google.com/search/_tra_/photo/AF1QipMukrWlHY7bPLYEr9dR3hJT9I6B_YxsnL4eo_pP
https://photos.google.com/search/_tra_/photo/AF1QipM-7sQJe0vWb-D9jx0ig4Hk0Wv0smLTkJkohg_G
Your answer
Follow this Question
Related Questions
Best possible way to implement physics on a chariot 2 Answers
UNET Multiplayer Vehicle Movement sync issues 0 Answers
having CharacterController movement in fixedupdate causes jerky movement on camera 0 Answers
How can I make my character walk while pressing arrow keys? 2 Answers
how create animation drag like this 0 Answers