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?
using UnityEngine;
using System.Collections;
public class SwingForce : MonoBehaviour {
public float forceStrength = 8f;
public float maxSpeed = 15f;
public float timePerOneSide = 2f;
private Rigidbody2D rb;
private Vector3 force;
private float timer = 0.0f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
timer = timePerOneSide;
}
void Update()
{
timer += Time.deltaTime;
if(timer >= timePerOneSide)
{
force = new Vector3(forceStrength *= -1, 0, 0);
timer = 0.0f;
}
if(rb.velocity.sqrMagnitude <maxSpeed)
rb.AddForce(force);
}
}
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.
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.