Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Rope Swing Force

Discussion in '2D' started by freedom667, Apr 26, 2016.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I have SwingForce script and it starting swing from left. but i couldn't swinging from right. I did forceStrength *= -1, 0, 0 but it doesn't forceStrength *= 1, 0, 0 what can i do for this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SwingForce : MonoBehaviour {
    5.  
    6.     public float forceStrength = 8f;
    7.     public float maxSpeed = 15f;
    8.     public float timePerOneSide = 2f;
    9.  
    10.     private Rigidbody2D rb;
    11.     private Vector3 force;
    12.     private float timer = 0.0f;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody2D>();
    18.         timer = timePerOneSide;
    19.  
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         timer += Time.deltaTime;
    26.         if(timer >= timePerOneSide)
    27.         {
    28.             force = new Vector3(forceStrength *= -1, 0, 0);
    29.             timer = 0.0f;
    30.         }
    31.         if(rb.velocity.sqrMagnitude <maxSpeed)
    32.             rb.AddForce(force);
    33.     }
    34.  
    35. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That syntax is invalid. Try this:

    Code (CSharp):
    1. void Update() {
    2.  
    3.     timer += Time.deltaTime;
    4.     if(timer >= timePerOneSide) {
    5.         force = new Vector3(-forceStrength, 0, 0);
    6.         timer = 0.0f;
    7.     }
    8.     if(rb.velocity.sqrMagnitude < maxSpeed)
    9.         rb.AddForce(force);
    10. }
     
  3. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    it doesn't work. same problem. it's going to right but not going to left
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I guess I didn't fully understand what you're trying to do with this code.

    is this what you were trying to do? It's like a toggle on a timer right?
    Code (CSharp):
    1. void Update() {
    2.  
    3.     timer += Time.deltaTime;
    4.     if(timer >= timePerOneSide) {
    5.         forceStrength *= -1;
    6.         force = new Vector3(forceStrength, 0, 0);
    7.         timer = 0.0f;
    8.     }
    9.     if(rb.velocity.sqrMagnitude < maxSpeed)
    10.         rb.AddForce(force);
    11. }
     
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    i swinging rope with this code. when it is forceStrength *= -1, rope begining swing from left. but i want to startin from right. I must write forceStrength *= 1, but doesn't work. it going to left after stopping at center and going to left again.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well if you start the rope in the center, then it will go to the left in "timePerOneSide" seconds. Then it will reverse and go to the right for "timePerOneSide" seconds, which will put it back at the center.

    So you need to start it on either the far left or far right, and get the timer long enough so that it will go all the way to the other side. Then it should loop correctly.
     
  7. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    i put -2 to timePerOneSide but it doesn't work again. stopped at center.