Search Unity

rotate 180 on a timer

Discussion in 'Scripting' started by keepthachange, Jun 5, 2015.

  1. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    i have ascript that makes things turn i dont want to get in to much on details cause its kinda of a secret atm
    but i need an object to rotate 180 at a certain time ...lets say for 3 mins then turn 180 and and over again in 3 mins i need it done in either js or c# ..

    any help is great
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Let me know if you have any questions :D

    Code (CSharp):
    1. float timer = 3;
    2. float degrees = 0;
    3.  
    4. void Update()
    5. {
    6.     if (timer > 0)
    7.     {
    8.         timer -= Time.deltaTime;
    9.         if (timer <= 0)
    10.         {
    11.             degrees = 180;
    12.         }
    13.     }
    14.     else
    15.     {
    16.         float rotateAmount = Mathf.Min (Time.deltaTime, degrees);
    17.         // put rotateAmount on the axis you want to rotate on for the next line
    18.         transform.Rotate(rotateAmount, 0, 0);
    19.         degrees -= Time.deltaTime;
    20.        
    21.         if (degrees <= 0)
    22.         {
    23.             timer = 3;
    24.         }
    25.     }
    26. }
     
    keepthachange likes this.
  3. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    okay i tryd this i guess i need to explain more i need to lerp to 180 when it hits 3 min mark stay and lerp back again when timer hits 3 mins again

    sorry just was pretty tierd yesterdsy

    and also i have a script for my player its in js and its the combo part it doesnt work properly like when i press down button animation plays but when i try to press button again for second attack it just cuts of the first animation like i need it to wait till one animation is done befor starting the next

    Code (JavaScript):
    1. #pragma strict
    2. var mainModel : GameObject;
    3.  
    4. var useMecanim : boolean = false;
    5. var attackPoint : Transform;
    6. var attackPrefab : Transform;
    7. enum whileAtk{
    8.         MeleeFwd = 0,
    9.         Immobile = 1,
    10.         WalkFree = 2
    11. }
    12. var whileAttack : whileAtk = whileAtk.MeleeFwd;
    13.  
    14. class LockOn{
    15.         var enable : boolean = true;
    16.         var radius : float = 5.0;  //this is radius to checks for other objects
    17.         var lockOnRange : float = 4.0; //this is how far it checks for other objects
    18.        
    19.         var lockTarget : Transform;
    20.    
    21.         var target : GameObject;
    22. }
    23. var autoLockTarget : LockOn;
    24.  
    25. class SkilAtk {
    26.         var icon : Texture2D;
    27.         var skillPrefab : Transform;
    28.         var skillAnimation : AnimationClip;
    29.         var skillAnimationSpeed : float = 1.0f;
    30.         var manaCost : int = 10;
    31.     }
    32. var skill : SkilAtk[] = new SkilAtk[3];
    33.  
    34. private var atkDelay : boolean = false;
    35. var freeze : boolean = false;
    36.  
    37. var attackSpeed : float = 0.15;
    38. private var nextFire : float = 0.0;
    39. var atkDelay1 : float = 0.1;
    40. var skillDelay : float = 0.3;
    41.  
    42. var attackCombo : AnimationClip[] = new AnimationClip[3];
    43. var attackAnimationSpeed : float = 1.0;
    44.  
    45. private var meleefwd : boolean = false;
    46.  
    47. var isCasting : boolean = false;
    48. var c : int = 0;
    49. private var conCombo : int = 0;
    50.  
    51.  
    52. var Maincam : Transform;
    53. var MaincamPrefab : GameObject;
    54. var attackPointPrefab : GameObject;
    55.  
    56. private var str : int = 0;
    57. private var matk : int = 0;
    58.  
    59. private var skillEquip : int  = 0;
    60.  
    61. class AtkSound{
    62.         var attackComboVoice : AudioClip[] = new AudioClip[3];
    63.         var magicCastVoice : AudioClip;
    64.         var hurtVoice : AudioClip;
    65. }
    66. var sound : AtkSound;
    67.  
    68. function Awake () {
    69.     gameObject.tag = "Player";
    70.     mainModel = GetComponent(Status).mainModel;
    71.     if(!mainModel){
    72.         mainModel = this.gameObject;
    73.     }
    74.     GetComponent(Status).useMecanim = useMecanim;
    75.    
    76.     var oldcam : GameObject[] = GameObject.FindGameObjectsWithTag("MainCamera");
    77.     for(var o : GameObject in oldcam){
    78.         Destroy(o);
    79.     }
    80.     var newCam : GameObject = Instantiate(MaincamPrefab, transform.position , MaincamPrefab.transform.rotation);
    81.     Maincam = newCam.transform;
    82.            
    83.     str = GetComponent(Status).addAtk;
    84.     matk = GetComponent(Status).addMatk;
    85.     //Set All Attack Animation'sLayer to 15
    86.     var animationSize : int = attackCombo.length;
    87.     var a : int = 0;
    88.     if(animationSize > 0 && !useMecanim){
    89.         while(a < animationSize && attackCombo[a]){
    90.             mainModel.animation[attackCombo[a].name].layer = 15;
    91.             a++;
    92.         }
    93.     }
    94.    
    95. //--------------------------------
    96.     //Spawn new Attack Point if you didn't assign it.
    97.     if(!attackPoint){
    98.         if(!attackPointPrefab){
    99.             print("Please assign Attack Point");
    100.             freeze = true;
    101.             return;
    102.         }
    103.         var newAtkPoint : GameObject = Instantiate(attackPointPrefab, transform.position , transform.rotation);
    104.         newAtkPoint.transform.parent = this.transform;
    105.         attackPoint = newAtkPoint.transform;  
    106.     }
    107.     //GetComponent(Status).hurt = GetComponent(PlayerAnimation).hurt;
    108.     if(sound.hurtVoice){
    109.             GetComponent(Status).hurtVoice = sound.hurtVoice;
    110.     }
    111. }
    112.  
    113.  
    114. function Update () {
    115.     var stat : Status = GetComponent(Status);
    116.     if(freeze || atkDelay || Time.timeScale == 0.0 || stat.freeze){
    117.         return;
    118.     }
    119.     var controller : CharacterController = GetComponent(CharacterController);
    120.     if (stat.flinch){
    121.         controller.Move(stat.knock * 6* Time.deltaTime);
    122.         return;
    123.     }
    124.        
    125.     if (meleefwd){
    126.         var lui : Vector3 = transform.TransformDirection(Vector3.forward);
    127.         controller.Move(lui * 5 * Time.deltaTime);
    128.     }
    129.     var bulletShootout : Transform;
    130. //----------------------------
    131.     //Normal Trigger
    132.         if (Input.GetKey("j") && Time.time > nextFire && !isCasting) {
    133.             TriggerAttack();
    134.         }
    135.         //Magic
    136.         if(Input.GetKeyDown("1") && !isCasting && skill[0].skillPrefab){
    137.             MagicSkill(0);
    138.         }
    139.         if(Input.GetKeyDown("2") && !isCasting && skill[1].skillPrefab){
    140.             MagicSkill(1);
    141.         }
    142.         if(Input.GetKeyDown("3") && !isCasting && skill[2].skillPrefab){
    143.             MagicSkill(2);
    144.         }
    145.        
    146. }
    147.  
    148. function TriggerAttack(){
    149.         if(freeze || atkDelay || Time.timeScale == 0.0 || GetComponent(Status).freeze){
    150.             return;
    151.         }
    152.         if (Time.time > nextFire && !isCasting) {
    153.             if(Time.time > (nextFire + 0.5)){
    154.                 c = 0;
    155.             }
    156.         //Attack Combo
    157.             if(attackCombo.Length >= 1){
    158.                 conCombo++;
    159.                 AttackCombo();
    160.             }
    161.         }
    162. }
    163.  
    164. function TriggerSkill(sk : int){
    165.         if(freeze || atkDelay || Time.timeScale == 0.0 || GetComponent(Status).freeze){
    166.             return;
    167.         }
    168.         if (Time.time > nextFire && !isCasting && skill[sk].skillPrefab) {
    169.             MagicSkill(sk);
    170.         }
    171.  
    172. }
    173.  
    174. function AttackCombo(){
    175.     if(!attackCombo[c]){
    176.         print("Please assign attack animation in Attack Combo");
    177.         return;
    178.     }
    179.     if(autoLockTarget.enable){
    180.         LockOnEnemy();
    181.     }
    182.     str = GetComponent(Status).addAtk;
    183.     matk = GetComponent(Status).addMatk;
    184.     var bulletShootout : Transform;
    185.     isCasting = true;
    186.     // If Melee Dash
    187.     if(whileAttack == whileAtk.MeleeFwd){
    188.             GetComponent(CharacterMotor).canControl = false;
    189.             MeleeDash();
    190.     }
    191.     // If Immobile
    192.     if(whileAttack == whileAtk.Immobile){
    193.             GetComponent(CharacterMotor).canControl = false;
    194.     }
    195.     if(sound.attackComboVoice.Length > c && sound.attackComboVoice[c]){
    196.             audio.clip = sound.attackComboVoice[c];
    197.             audio.Play();
    198.         }
    199.     while(conCombo > 0){
    200.         if(!useMecanim){
    201.             //For Legacy Animation
    202.             mainModel.animation.PlayQueued(attackCombo[c].name, QueueMode.PlayNow).speed = attackAnimationSpeed;
    203.             var wait : float = mainModel.animation[attackCombo[c].name].length;
    204.         }else{
    205.             //For Mecanim Animation
    206.             GetComponent(PlayerMecanimAnimation).AttackAnimation(attackCombo[c].name);
    207.             var clip = GetComponent(PlayerMecanimAnimation).animator.GetCurrentAnimationClipState(0);
    208.             wait = clip.Length -0.5;
    209.         }
    210.    
    211.     yield WaitForSeconds(atkDelay1);
    212.     c++;
    213.    
    214.     nextFire = Time.time + attackSpeed;
    215.             bulletShootout = Instantiate(attackPrefab, attackPoint.transform.position , attackPoint.transform.rotation);
    216.             bulletShootout.GetComponent(BulletStatus).Setting(str , matk , "Player" , this.gameObject);
    217.             conCombo -= 1;
    218.            
    219.     if(c >= attackCombo.Length){
    220.         c = 0;
    221.         atkDelay = true;
    222.         yield WaitForSeconds(wait);
    223.         atkDelay = false;
    224.     }else{
    225.         yield WaitForSeconds(attackSpeed);
    226.     }
    227.    
    228.     }
    229.    
    230.     //yield WaitForSeconds(attackSpeed);
    231.     isCasting = false;
    232.     GetComponent(CharacterMotor).canControl = true;
    233. }
    234.  
    235. function MeleeDash(){
    236.     meleefwd = true;
    237.     yield WaitForSeconds(0.2);
    238.     meleefwd = false;
    239.  
    240. }
    241.  
    242. //---------------------
    243. //-------
    244. function MagicSkill(skillID : int){
    245.     if(!skill[skillID].skillAnimation){
    246.         print("Please assign skill animation in Skill Animation");
    247.         return;
    248.     }
    249.     if(autoLockTarget.enable){
    250.         LockOnEnemy();
    251.     }
    252.     str = GetComponent(Status).addAtk;
    253.     matk = GetComponent(Status).addMatk;
    254.    
    255.     if(GetComponent(Status).mana < skill[skillID].manaCost || GetComponent(Status).silence){
    256.         return;
    257.     }
    258.     if(sound.magicCastVoice){
    259.         audio.clip = sound.magicCastVoice;
    260.         audio.Play();
    261.     }
    262.     isCasting = true;
    263.     GetComponent(CharacterMotor).canControl = false;
    264.    
    265.     if(!useMecanim){
    266.         //For Legacy Animation
    267.         mainModel.animation[skill[skillID].skillAnimation.name].layer = 16;
    268.         mainModel.animation[skill[skillID].skillAnimation.name].speed = skill[skillID].skillAnimationSpeed;
    269.         mainModel.animation.Play(skill[skillID].skillAnimation.name);
    270.         var wait : float = mainModel.animation[skill[skillID].skillAnimation.name].length -0.3;
    271.     }else{
    272.         //For Mecanim Animation
    273.         GetComponent(PlayerMecanimAnimation).AttackAnimation(skill[skillID].skillAnimation.name);
    274.         var clip = GetComponent(PlayerMecanimAnimation).animator.GetCurrentAnimationClipState(0);
    275.         wait = clip.Length -0.3;
    276.     }
    277.        
    278.     nextFire = Time.time + skillDelay;
    279.     //Maincam.GetComponent(ARPGcamera).lockOn = true;
    280.     var bulletShootout : Transform;
    281.    
    282.     yield WaitForSeconds(wait);
    283.     //Maincam.GetComponent(ARPGcamera).lockOn = false;
    284.     bulletShootout = Instantiate(skill[skillID].skillPrefab, attackPoint.transform.position , attackPoint.transform.rotation);
    285.     bulletShootout.GetComponent(BulletStatus).Setting(str , matk , "Player" , this.gameObject);
    286.     yield WaitForSeconds(skillDelay);
    287.     isCasting = false;
    288.     GetComponent(CharacterMotor).canControl = true;
    289.     GetComponent(Status).mana -= skill[skillID].manaCost;
    290. }
    291.  
    292. // Lock On the closest enemy
    293. function LockOnEnemy() {
    294.     var checkPos : Vector3 = transform.position + transform.forward * autoLockTarget.lockOnRange;
    295.     var closest : GameObject;
    296.    
    297.     var distance : float = Mathf.Infinity;
    298.     var position : Vector3 = transform.position;
    299.        autoLockTarget.lockTarget = null; // Reset Lock On Target
    300.        var objectsAroundMe : Collider[] = Physics.OverlapSphere(checkPos , autoLockTarget.radius);
    301.         for(var obj : Collider in objectsAroundMe){
    302.             if(obj.CompareTag("Enemy")){
    303.                var diff : Vector3 = (obj.transform.position - position);
    304.                var curDistance : float = diff.sqrMagnitude;
    305.                if (curDistance < distance) {
    306.                //------------
    307.                  closest = obj.gameObject;
    308.                  distance = curDistance;
    309.                  autoLockTarget.target = closest;
    310.                  autoLockTarget.lockTarget = closest.transform;
    311.                }
    312.             }
    313.         }
    314.         //Face to the target
    315.         if(autoLockTarget.lockTarget){
    316.                 var lookOn : Vector3 = autoLockTarget.lockTarget.position;
    317.                  lookOn.y = transform.position.y;
    318.                   transform.LookAt(lookOn);
    319.         }
    320.    
    321. }
     
  4. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    what im trying to do with the rotate scrip is make my background wall turn around when the sun goes down to a night wall then when the sun comes up a morning wall im changing the game up for the last time everything the player interacts with is 3d the background is 2d or like 2d like stars and clouds
     
  5. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    i think it look pretty cool actually sorta reminds me off LBP
     
  6. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I'm sorry, I wasn't thinking clearly when I posted that. Use my same code, but in the two places it says "timer =3;" make it "timer =180;" (180seconds = 3minutes).

    you'll also probably want to make your background rotate faster, so change this:
    Code (CSharp):
    1. float rotateAmount = Mathf.Min (Time.deltaTime, degrees);
    2. transform.Rotate(rotateAmount, 0, 0);
    3. degrees -= Time.deltaTime;
    To this:
    Code (CSharp):
    1. float rotateSpeed = 10; //change this if you'd like
    2. float rotateAmount = Mathf.Min (Time.deltaTime * rotateSpeed, degrees);
    3. transform.Rotate(0, rotateAmount, 0);
    4. degrees -= Time.deltaTime * rotateSpeed;
    As for your combo+animation problem, I don't have the time to dig deep into it right now, but in the animator you want to make a transition from attack1 to attack2, and have it transition when the attack1 animation finishes. That way, in code, you can tell the second attack to start while the first attack is going, and it won't start until the first attack is done.
     
  7. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    Thank you for your help with the Rotate script i will be useing it in my game as you can see im still working on the lines between the cubes but it looks good so far heres a link

    to see what it look like and i wanted to ask if you could help me with one more script atm im trying to make and object(star.etc) attached to a rope fall out of the sky when it turns dark and when the sun comes up make it go back up to the sky so it out of camera view



    so sun comes up star goes back up sun goes down stars come back down
    the stars are conected to a rope like i said it has all physics allready just need it to go up and down at a certain time