0
\$\begingroup\$

I followed the code at this YouTube channel: https://www.youtube.com/watch?v=OR0e-1UBEOU&t=4178s, and did some modification on the code so that the scale of the bird will change during projectile using the absolute sine function under the if statement.

I'm using the scale (16/20)*|sin(distance)| for my x and y component. However, when I launch the game, the scale stays as 0 along the path. Why is my scale staying as a constant zero?

using UnityEngine;
using UnityEngine.Assertions.Must;
using UnityEngine.SceneManagement;

public class Bird : MonoBehaviour
{   
    Vector3 OldPosition;
    Vector3 DistanceDifference;
    float TotalDistance = 0;
    Vector3 _initialPosition;
    private bool _birdWasLaunched;
    [SerializeField] private float _launchPower = 500;

    void start()
    {
        OldPosition = transform.position; 
    }
    
    private void Awake()
    {
        _initialPosition = transform.position;

    }

    void Update()
    {   
        if (_birdWasLaunched && GetComponent<Rigidbody2D>().velocity.magnitude > 0.1)
        {
            DistanceDifference = transform.position - OldPosition;
            TotalDistance += DistanceDifference.magnitude;
            OldPosition = transform.position;
            transform.localScale = new Vector3((16/20)*Mathf.Abs(Mathf.Sin(TotalDistance/5)), (16/20)*Math.Abs(Mathf.Sin(TotalDistance /5)) , Mathf.Sin(TotalDistance /5));   
        }

        if (transform.position.y > 15.00 || transform.position.y < -15.00 || transform.position.x > 18 || transform.position.x < -18)
        {
            string currentSceneName = SceneManager.GetActiveScene().name;
            SceneManager.LoadScene(currentSceneName);
        }
    }

    void OnMouseDown()
    {
        GetComponent<SpriteRenderer>().color = Color.red;
    }

    private void OnMouseUp()
    {
        GetComponent<SpriteRenderer>().color = Color.white;
        Vector2 directionToInitialPosition = _initialPosition - transform.position;
        GetComponent<Rigidbody2D>().AddForce(directionToInitialPosition * _launchPower);
        GetComponent<Rigidbody2D>().gravityScale = 1;
        _birdWasLaunched = true;
    }

    private void OnMouseDrag()
    {
        Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(newPosition.x, newPosition.y);
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ "How can 0.8|sin(distance)| getting 0 along the path?" - Because sin(distance) can be zero (or close to)? What is the expected behaviour you want? \$\endgroup\$
    – Tyyppi_77
    Oct 27 '20 at 14:29
  • \$\begingroup\$ I mean the bird icon entirely gone as soon as I launch it. I checked the scale, it display 0 value along the whole path without varying. \$\endgroup\$
    – chuackt
    Oct 27 '20 at 14:38
  • 2
    \$\begingroup\$ (16/20) is integer division, resulting in 0. Does replacing that with 0.8 fix the issue? \$\endgroup\$
    – Tyyppi_77
    Oct 27 '20 at 14:45
  • \$\begingroup\$ Okay that make sense I thought (16/20)=0.8 in programming code :D. I use 0.8f fixed the issue. Thanks. \$\endgroup\$
    – chuackt
    Oct 27 '20 at 14:57
3
\$\begingroup\$

(16/20) is integer division, resulting in 0. Replace that with 0.8f or (16/20.0f).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.