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 EpicEra · May 11, 2013 at 07:50 PM · rotationgameturret

Need Help with random rotatition on game turret

Hi everyone! At the moment me and my buddys are developing a PC game and need some help with the programing. I have a turret that shoots the player, code works fine but I need help with random rotation. I want the turret to rotatate random when the player is at a distance. Like, a little bit to the left... then a little bit to the right, and to the right a little more maybe :) As if it were looking around for something.

I have done the code so it works all the way exept this part. So here is my else statement that need the random rotation.

else

             {
                     transform.rotation.y += Random.Range(-1,1);
                             
             }

This ^ did not work, It just rotates once then stops. Have it in the update()

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 robertbu · May 12, 2013 at 12:35 AM 1
Share

'transform.rotation is a Quaternion...a non-intuitive, 4D construct. Unless you fully understand the math behind them, you don't want to address the individual x,y,z,w components individually. You can use transform.eulerAngles, but you should never assign individual axes, nor should you depend on any specific eular angle representation. For example you could assign (180,0,0) and immediately read it back and get (0,180,180) which is the same 'physical' rotation in a different euler representation. The easiest way to deal with this issue is to treat eulerAngles as 'write-only'. That is keep your own Vector3. When you want to change an angle, change it in the Vector3 and then assign the Vector3. If you need to read an angle, read it from your own Vector3.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Brahim113 · May 11, 2013 at 09:05 PM

This is in the logic that i got from your code

         int rotate = Random.Range(-1,2);
         transform.Rotate(0,rotate,0,Space.Self);

It will stand still. Rotate to the left or right do a random new number every frame. But it will look rly retarded (atleast what i saw when i tried it out)

I would suggest you to atleast make it rotate in 1 second or more until it changes in a new random.

I think this is something you are looking for. It is not perfect but it is something you could work on

 using UnityEngine;
 using System.Collections;
 
 public class PlayWithRotation : MonoBehaviour {
 
     int _bFlag;
     float _currentTime;
     float _delay = 1.0f;
 
     
     
     void Start () 
     {
         _currentTime = Time.time;
         
     }
     
     
     void RotateTurret(int rot)
     {
         transform.Rotate(0,rot,0,Space.Self);        
     }
     
     void Update () 
     {
         if(Time.time > _currentTime)
         {
             _currentTime += _delay;
             //_bFlag will be -1 to 1
             _bFlag = Random.Range(-1,2);
             
         }
         RotateTurret(_bFlag);
     }
 }


Hope this helps you out!

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 EpicEra · May 15, 2013 at 07:01 PM 0
Share

Did not understand any of those solutions :/ Im a noob programmer and we are under a thight deadline, so this became my sulotion:

transform.Rotate(Time.deltaTime * 6, 0, 0, Space.World);

Now it only rotates one way as idle animation, but that will do i guess. And I dont really understand most of the stuff u guys mentioned about eluer.angels, vector 3 and Quaternion. So *I guess I have to read up on it a little, when i get time :)

thanks

avatar image
0

Answer by robertbu · May 15, 2013 at 07:52 PM

Here are two more solutions. Attach them to a block in an empty scene to start.

 #pragma strict
 var MinAngle = -70.0;
 var MaxAngle = 70.0;
 var timestamp : float;
 var recalcFreq = 1.0;  // Seconds before selecting a new angle 
 var speed = 35.0;
 var qTo : Quaternion;
 
 var randomSeeking = true;
 
 function Update () {
     if (randomSeeking) {
         if (timestamp < Time.time) {
             timestamp = timestamp + recalcFreq;
             qTo = Quaternion.Euler(0.0, Random.Range(MinAngle, MaxAngle), 0.0);
         }
         transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
     }
 }

This second one produces a smoother seeking:

 #pragma strict
 var neutralAngle = 0.0;  // Angle that is midway between the min and max angle.
 var angleRange = 70.0;   // Delta (both sides) of the neutral Angle.
 var trigSpeed1 = 0.2;
 var trigSpeed2 = 0.5;
 var qTo : Quaternion;
 var speed = 20.0;         // Speed of the rotation.
 
 var randomSeeking = true;
 
 function Update () {
     if (randomSeeking) {
             var angle = neutralAngle + (Mathf.Sin(trigSpeed1 * Time.time) + Mathf.Sin(trigSpeed2 * Time.time)) / 2.0 * angleRange;
 
             qTo = Quaternion.Euler(0.0, angle, 0.0);
     }
     //transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
     transform.rotation = Quaternion.Lerp(transform.rotation, qTo, Time.deltaTime * speed);
 }
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

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

Multiple Cars not working 1 Answer

Rotate Turret to Tag 1 Answer

How To make game installer ? 3 Answers

When I rotate my player in open 3 space, the transform rotation component keeps snapping back to origin. However, when I disable my rigidbody.rotate command, the transform rotation works just fine. 0 Answers

Beginner need some help on Card Game 2 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