Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

a swing simulation without rigidbody

Discussion in 'Scripting' started by mirkojpn, Aug 11, 2018.

  1. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    hi everyone.
    i'm going to create a simple game where the player are going to use a swing but is like a 360 degrees swing, so for every time that you go upd and down you'll accumulate velocity than you will be able to rotate for 360 degrees, i'm trying to figure out to how do it on unity without a rigidbody.
    any advice?
    i'm not looking for a ready to use code.
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You could build up the swing momentum by using Ping Pong and increasing the length. Once it has reached 360, then just lerp the angle going forward.
     
  3. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    thank's you for your answer.
    actually what i'm trying to do is...

    with the up arrow and the down arrow i would be able to play with the swing, the accumulate velocity for the 360 degree one.
    this is my goal.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Create an empty object in your scene and call it "Pivot". Then create a second object and call it "Swing". Move the Swing an appropriate distance beneath the Pivot in the world scene. Then, in the object hierarchy, make Swing be a child of Pivot.

    Now all you need to do is play around with the transform.rotation of Pivot and you have the beginnings of your game.

    Try adding this in a script to Pivot, you'll see what I mean:
    Code (CSharp):
    1. void Update()
    2. {
    3.     var temp = transform.rotation.eulerAngles;
    4.     temp.z = Mathf.PingPong(Time.time, 1f) * 90f - 45f;
    5.     transform.rotation = Quaternion.Euler(temp);
    6. }
     
  5. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    thank's you! the anchor with the pvot object is exactly what i was looking for!
    i'll complete it, for now really thank's you!
     
    Doug_B likes this.
  6. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    actually this is what i have, but on get key it will start from 5.0f not from 0, and i'll try to go back with the down arrow key, it switch just from 40 to -45...i can't figure out how to do it, any help?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Swing : MonoBehaviour
    6. {
    7.     private const float _speed = 10.0f;
    8.     private float _MAX_ROTATION = 45.0f;
    9.     private float _MIN_ROTATION = -45.0f;
    10.     private Vector3 _vectorZ;
    11.     private float _moveZ;
    12.  
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.        
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         Quaternion currentRotation = gameObject.transform.rotation;
    25.         UpdateRotation(currentRotation);
    26.         Movement(_moveZ);
    27.     }
    28.  
    29.     private void UpdateRotation (Quaternion _currentrotation)
    30.     {
    31.         _moveZ = _moveZ + _speed * Time.deltaTime;
    32.         _moveZ = Mathf.Clamp(_moveZ,_MIN_ROTATION,_MAX_ROTATION);  
    33.     }
    34.  
    35.     private void Movement (float movez)
    36.     {
    37.         _vectorZ = new Vector3(0,0,movez);
    38.        
    39.         if (Input.GetKey(KeyCode.UpArrow))
    40.         {
    41.             Debug.Log(_moveZ);
    42.             gameObject.transform.rotation = Quaternion.Euler(_vectorZ);
    43.         }
    44.         if (Input.GetKey(KeyCode.DownArrow))
    45.         {
    46.             Debug.Log(_moveZ);
    47.             gameObject.transform.rotation = Quaternion.Euler(-_vectorZ);
    48.         }
    49.  
    50.     }
    51.  
    52.  
    53.  
    54. }
    55.  
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I think first, we need to be completely clear on what is trying to be achieved here. So when the player presses the up / down arrows, what exactly is the expected result? Is it :-
    1. The swing speed is increased but not the min / max rotations.
    2. The min / max rotations are increased (in abs terms) but not the swing speed.
    3. The swing speed is physically affected. That is to say if the swing is rising and player presses up, their speed is positively influnced (which may mean increasing speed or decreasing the retardation). If they are rising and the down arrow is pressed, then the opposite effect as just described.
    4. Something else.
     
  8. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126


    i'm trrying to replicate something like this, for every step you'll be able to accumulate velocity, by go forward,

    so i think it's the number 3
     
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I see. So basically, you are going to have to read up on the maths behind a swinging pendulum and implement that. :)

    Incidentally, why are you not using rigid bodies and the physics engine for this?
     
  10. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    yes, should be hard, but i'll do my best.

    actually i'm studying in Japan, and they here simply don't want to use pre made component, you should to be able to create it yourself...
     
    Doug_B likes this.
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I see. Well it will certainly be valuable learning. I'm sure you will do fine with it. :)
     
  12. mirkojpn

    mirkojpn

    Joined:
    Mar 31, 2018
    Posts:
    126
    yes, i'll do my best, for now, again thank you for your help!