Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MonkeyAssassin8 · Sep 27, 2010 at 11:33 AM · directionturretoneshoots

Turret doesn't work properly C#

I have made my own turret using C#, It works as it rotates towards the closest target who is in range and shoots them. my problem is; the child object which spawns the bullet does not rotate with the turret and therefore is always shooting in one direction.

Here is my Turret code:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Targeting : MonoBehaviour { public List<Transform> targets; public Transform selectedTarget; public Transform myTransform; public Transform objBullet; public float attackTimer; public float coolDown;

void Start(){ targets = new List<Transform>(); AddAllEnemies(); selectedTarget = null; myTransform = transform;

 attackTimer = 0;
 coolDown = 0.1f;

}

public void AddAllEnemies(){ targets.Clear(); //this is here as enemies spawn and it needs to be reset each time GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");

 foreach(GameObject enemy in go)
     AddTarget(enemy.transform);

}

public void AddTarget(Transform enemy){

 targets.Add(enemy);

}

public void SortTargetsByDistance(){ targets.Sort(delegate(Transform t1, Transform t2){ return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); }); }

void Update(){ AddAllEnemies(); TargetEnemy();

 transform.rotation = Quaternion.LookRotation(selectedTarget.transform.position-transform.position);

 if(attackTimer &gt; 0f)
     attackTimer -= Time.deltaTime;

 if(attackTimer &lt; 0f)
     attackTimer = 0f;

     if(attackTimer == 0f){ //this is where the turret shoots
 if ( Vector3.Distance(selectedTarget.transform.position,transform.position) &lt; 10f ){
     GameObject objCreatedBullet = (GameObject) Instantiate(objBullet, transform.Find("TurretBulletSpawn").transform.position, Quaternion.identity);
     Physics.IgnoreCollision(objCreatedBullet.collider, collider);
     objCreatedBullet.rigidbody.AddForce(transform.forward * 200);


     attackTimer = coolDown;
 }

} }

public void TargetEnemy(){ SortTargetsByDistance(); selectedTarget = targets[0]; }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MonkeyAssassin8 · Sep 27, 2010 at 12:04 PM 0
Share

Ive narrowed it down to it being a problem with: GameObject objCreatedBullet = (GameObject) Instantiate(objBullet, transform.Find("TurretBulletSpawn").transform.position, Quaternion.identity);

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by spinaljack · Sep 27, 2010 at 12:09 PM

The problem is here

Instantiate(objBullet, transform.Find("TurretBulletSpawn").transform.position, Quaternion.identity);

First of all I wouldn't use transform.Find() every time you fire a bullet, much better to just save a variable for bullet spawn, put the Find() in the start function and then save the result.

The second problem is the rotation argument, you've set it to Quaternion.identity, obviously this means that the bullet will always appear facing the same direction (0,0,0)

What you need to do is use your turret's rotation in that bit e.g.

Instantiate(objBullet, bulletSpawn.position, transform.rotation);

Where bullet spawn is the variable where you saved the Transform of game object where you want the bullet to appear.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MonkeyAssassin8 · Sep 27, 2010 at 12:09 PM

OK, I fixed the problem; I should have put transform.rotation instead of Quaternion.identity as I found out that Quaternion.identity was no rotation.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spinaljack · Sep 27, 2010 at 12:11 PM 0
Share

the bit about the transform.Find still applies

avatar image
0

Answer by Alter · Jul 24, 2012 at 07:33 PM

Hi guys, currently working with this script but I'm having an issue. The bullets wont go away they just keep spawning and spawning :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Alter · Jul 24, 2012 at 07:35 PM

EDIT: Also the bullet stays in one spot

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Turret : MonoBehaviour {

public List targets; public Transform selectedTarget; public Transform myTransform; public Transform objBullet; public float attackTimer; public float coolDown = 5; public Transform bulletSpawn;

void Start(){ targets = new List(); AddAllEnemies(); selectedTarget = null; myTransform = transform;

 attackTimer = 0;
 coolDown = 1f;

}

public void AddAllEnemies(){ targets.Clear(); //this is here as enemies spawn and it needs to be reset each time GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");

 foreach(GameObject enemy in go)
     AddTarget(enemy.transform);

}

public void AddTarget(Transform enemy){

 targets.Add(enemy);

}

public void SortTargetsByDistance(){ targets.Sort(delegate(Transform t1, Transform t2){ return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); }); }

void Update(){ AddAllEnemies(); TargetEnemy();

 transform.rotation = Quaternion.LookRotation(selectedTarget.transform.position-transform.position);

 if(attackTimer > 0f)
     attackTimer -= Time.deltaTime;

 if(attackTimer < 0f)
     attackTimer = 0f;

     if(attackTimer == 0f){ //this is where the turret shoots
 if ( Vector3.Distance(selectedTarget.transform.position,transform.position) < 10f ){
     GameObject objCreatedBullet = (GameObject) Instantiate(objBullet, bulletSpawn.position, transform.rotation);
     Physics.IgnoreCollision(objCreatedBullet.collider, collider);
     objCreatedBullet.rigidbody.AddForce(transform.forward * 500);


     attackTimer = coolDown;
 }

} }

public void TargetEnemy(){ SortTargetsByDistance(); selectedTarget = targets[0]; }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

3D nested Turret Prefab Rotation 1 Answer

Set bullet direction according to turret direction 0 Answers

How do i move CharacterController toward the direction its facing? 0 Answers

Changing the direction of an object using another object. 0 Answers

Calculate weighted mean based on direction 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges