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
0
Question by orcinusdev · Oct 03, 2013 at 09:55 AM · c#turretsimple

C# Simple turret script problem

Hello!

I have the simplest Turret code which doesn't work. It fires projectiles in Z direction (?!) When player is at the positive x positin of the turret the turret shoots upwards in Z, otherwise downwards. It is always facing the player however. What is wrong?

I attached this code to a simple cube. And the target is a moving player.

 using UnityEngine;
 using System.Collections;
 
 public class TurretScript : MonoBehaviour {
 
 public GameObject projectile;
 public Transform target;
     
     void Start(){
     
         
     RepeatShooting();
     }
 
     void Update(){
     
     transform.rotation = Quaternion.LookRotation (target.position - transform.position);
             
     }
     // Shoot and repeat
     void RepeatShooting () {
         InvokeRepeating ("LaunchProjectile", 2, 0.3f);
 
         
     }
     
     public void LaunchProjectile(){
         
         //Vector3 projectileStart = new Vector3(
         
         Instantiate (projectile, transform.position, transform.rotation);
     }    
     
     
 }
 
Comment
Add comment · Show 3
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 Tanshaydar · Oct 03, 2013 at 11:19 AM 0
Share

This depends on your projectile. You should hold it in a variable like GameObject projectile = Instantiate... and then Quaterniate it according to player position. Your prefab will always act same.

avatar image orcinusdev · Oct 03, 2013 at 12:19 PM 0
Share

I have tried that. $$anonymous$$y additional code:

GameObject newProjectile = (GameObject) Instantiate (projectile, transform.position, transform.rotation); newProjectile.transform.rotation = Quaternion.LookRotation (target.position - transform.position);

This give me same results as before.

When I use

 Transform.Rotate(Vector3.forward);


as rotation ins$$anonymous$$d, I get a nice spray of projectiles just in the direction of the turret. So I suppose, the Quaternion.LookRotation is messing it up?

avatar image robertbu · Oct 03, 2013 at 02:47 PM 0
Share

I spot no problems in this code (assu$$anonymous$$g that 'target' is initialized correctly). That means the problem is most likely in the code that moves the projectile or you have some sort of collision between the projectile and some collider. Nothing here move the projectile.

1 Reply

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

Answer by Calum-McManus · Oct 03, 2013 at 04:05 PM

Try making a spawn at the end of the barrel of the turret and using something like this:

 Instantiate(Projectile, Spawn.transform.position, Spawn.transform.rotation);

Also use something like this:

  Vector3 vSpeedDelta = transform.forward * (Time.deltaTime * ProjectileSpeed);
         transform.position += (vSpeedDelta);

Put this in the update of a projectile script and attach that new script to the projectile itself. This should always fire straight assuming the projectile spawn has its forward axis points forward of the barrel.

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 orcinusdev · Oct 03, 2013 at 04:52 PM 0
Share

Trank you all for helping me. The projectile was the problem as you mentioned. It was set to Vector3.right, silly me!

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Simple question about targetting 0 Answers

Rotating Tank Turret with correct angle 3 Answers

PlayerPrefs string to load scene (edited) 1 Answer


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