Advertisement

moving an object in C#

Started by July 30, 2018 02:03 AM
2 comments, last by Wayne Allen 6 years, 1 month ago

Ok so I am trying to apply a random up / down and force to a rigid body, and after the force is applied after x seconds it reset back to the middle. This is the code I wrote but the rigid body does not stay in the screen max height IE; if up or down force gets applied in a row it will go off camera. I hope I'm explaining this enough. Below are my 1st attempt and I am curious how far off am I? should I just give up on the game dev dream lol. the picture is for visual reference only not actual graphics ;) 


public class upDownMovement : MonoBehaviour {
    public Rigidbody devilRB;
    private float resetPos;

    void Start ()
    {
        StartCoroutine(TimedMovement());
   
    }
	

   private IEnumerator TimedMovement()
    {

        yield return new WaitForSeconds(3);
        float randomHeight = Random.Range(-Screen.height, Screen.height);
        if (randomHeight < 1)
        {
            Debug.Log("Going Down");
            resetPos = (Mathf.Sign(randomHeight));
        }

        if (randomHeight > 0)
        {
            Debug.Log("Going Up");
            resetPos = (Mathf.Sign(-randomHeight));
        }

        Debug.Log("force to applie: "+randomHeight);
        Debug.Log("reverse force: "+resetPos);
        devilRB.AddForce(000, randomHeight, 000);
        StartCoroutine(TimedReset());
    }

    private IEnumerator TimedReset()
    {

        yield return new WaitForSeconds(3);
        devilRB.AddForce(000, resetPos, 000);
        StartCoroutine(TimedMovement());
    }
}

1.png.625a2ecb99140c71b75280f6e1116876.png

3.png.7e3477ba7b3b3cf45a81452e3ae6a6f7.png2.png.e5628d61c33946b514ef72f956b37dd4.png

I really didn't think I would run into so many coding issues as I have PHP / action script under my belt ? 

Ok so after some digging in unity API, I think this is working now that I have changed this section:


        if (randomHeight < 1)
        {
            Debug.Log("Going Down");
            resetPos = (Mathf.Abs(+randomHeight));
        }

        if (randomHeight > 0)
        {
            Debug.Log("Going Up");
            resetPos = -randomHeight;
        }

I think the problem was Mathf.Sign should have been Maths.Abs 

Advertisement

Ok, so the final code for the movement is...


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class upDownMovement : MonoBehaviour {
    public Rigidbody devilRB;
    private float resetPos;
  
    // Use this for initialization
    void Start ()
    {
        StartCoroutine(TimedMovement());
    }
	
   private IEnumerator TimedMovement()
    {
        // Wait
        yield return new WaitForSeconds(3);

        // Get the screen height and store in randomHeight float
        float randomHeight = Random.Range(-Screen.height, Screen.height);

        // Restrict up and down to keep in camera view

        if (randomHeight > 210) // Max up force
        {  
            randomHeight = 210;
        }

        if (randomHeight < -210) // Max down force
        {  
            randomHeight = -210;
        }


        // Setting the reset force
        if (randomHeight < 1) 
        {
            Debug.Log("Going Down");
            resetPos = (Mathf.Abs(+randomHeight));
        }

        if (randomHeight > 0)
        {
            Debug.Log("Going Up");
            resetPos = -randomHeight;
        }

        // Debug logs to view applied forces
        Debug.Log("force to apply: "+randomHeight);
        Debug.Log("reverse force: "+resetPos);

        // Applying the up and down force to regidbody
        devilRB.AddForce(000, randomHeight, 000);
    
        // Call to reset the force applied.
        StartCoroutine(TimedReset());
    }

    // Resetting timer to put rigidbody back to starting position
    private IEnumerator TimedReset()
    {
        // Wait
        yield return new WaitForSeconds(3);

        // Apply the reset force to the regidbody
        devilRB.AddForce(000, resetPos, 000);

        // Restart the process
        StartCoroutine(TimedMovement());
    }
}

I have added a max up/down force to add padding for the screen margins. I am probably certain this is not the best approach but it does seem to work. I have added a constant force component to the rigid body so it always goes forward as well. Would anyone else do anything different?

This topic is closed to new replies.

Advertisement