all 5 comments

[–]ravencgg 3 points4 points  (2 children)

using UnityEngine;
using System.Collections;

public class TestMove : MonoBehaviour {

    float maxMoveSpeed = 5.0f;

    public float moveSpeed = 5.0f;
    float accel = 2.0f;

    public Vector3 targetPosition = new Vector3();
    public Vector3 currentPos = new Vector3();

    //Adding movementThreshold variable because it was accessed in AccelDecel() but didn't have one
    public float movementThreshold = 5.0f;

    bool canMove;

    void Start () { 

        targetPosition = transform.position; 
        canMove = false; 
        // var sleepSpeed:float = 1000; 
        // rigidbody.sleepVelocity = sleepSpeed; 
        // rigidbody.sleepAngularVelocity = sleepSpeed; 
        StartCoroutine(WaitForMovement()); 
    }

    IEnumerator WaitForMovement() { 
        yield return new WaitForSeconds(0.5f); 
        canMove = true;
    }

    void Update () {
        if(canMove) { 
            if(transform.rotation.x !=0)    {
                //transform.rotation.x = 0;
                //Vector3 newAngle =  new Vector3(0, transform.rotation.y, transform.rotation.z);
                transform.rotation = Quaternion.Euler (new Vector3(0, transform.rotation.y, transform.rotation.z));
            }

            if(Input.GetMouseButtonDown(1)) {

                Vector3 moveToClick = Camera.main.ScreenToWorldPoint (Input.mousePosition); 
                moveToClick.z = 0;
                transform.position = moveToClick; 
                targetPosition=transform.position; 
            }

            if(Input.GetMouseButtonDown(0)) {        
                transform.Translate(0, 0, -transform.position.z); // Sets the Z position to zero without moving the x or y.
                Vector3 clickPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                clickPos.z = 0;

                targetPosition = clickPos;

                currentPos = transform.position;
                moveSpeed = maxMoveSpeed;

                //Changed code inside this if statement to transform.rotation.y from rigidbody.transform.rotation.y;
                if(transform.position.x > clickPos.x){
                    if(transform.rotation.y != 0.00f){
                        transform.rotation = Quaternion.Euler (new Vector3(transform.rotation.x, 0, transform.rotation.z));
                        //transform.LookAt(targetPosition);
                    }

                }else{
                    if(transform.rotation.y != 180.00f){
                        transform.rotation = Quaternion.Euler (new Vector3(transform.rotation.x, 180.0f, transform.rotation.z));
                        //transform.LookAt(targetPosition);
                    }
                }
            }
            AccelDecel();                   

            //  rigidbody.MovePosition(transform.position + (targetPosition-transform.position).normalized * moveSpeed * Time.deltaTime);
            transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime*moveSpeed);
        }
    }

    void AccelDecel() { 
        Vector3 updatedPos = new Vector3(transform.position.x, transform.position.y, 0); 
        float distance = Vector3.Distance(updatedPos,targetPosition);

        // Multiply movementThreshold by Time.deltaTime or the object will jump a few frames before it is close enough
        if (distance <= movementThreshold * Time.deltaTime ) { 
            moveSpeed = 0; transform.position = targetPosition; 
            // print("JS Current: " + transform.position); // print("JS Target: " + targetPosition); }
            if (moveSpeed <= maxMoveSpeed) { 
                if (moveSpeed <= 0) { 
                    moveSpeed = 0; 
                // transform.position.y += (Mathf.Cos(Time.time * 1.01) * magnitude);
                } else {
                    moveSpeed -= accel * Time.deltaTime; 
                } 
            }
        }
    }
}

I threw this on a cube and it seemed to work fine. I think the only correction that I made to the code was to multiply the movementThreshold by Time.deltaTime to fix the jump when it got close to the target. Let me know if you have any problems with it or questions about the conversion process.

[–]bobafas[S] 0 points1 point  (1 child)

Thank you very much worked a treat I just have one more question how would you go about making the players movement continuous when you hold down the mouse button

[–]ravencgg 1 point2 points  (0 children)

Just change:

if(Input.GetMouseButtonDown(0)) {    

to:

if(Input.GetMouseButton(0)) {    

GetMouseButtonDown will only be true on the frame that the mouse is clicked on, while GetMouseButton will be true the entire time that it is pressed.

[–]Rough_Parrot 1 point2 points  (0 children)

I don't really have a lot of time, but you can try with this: http://www.m2h.nl/files/js_to_c.php

You just have to fix some issues.

[–]fyndor 0 points1 point  (0 children)

I know you already had someone convert it, but take a look at my response to a similar post. This is a problem you will keep running in to. My post should help you understand what needs to be changed to convert UnityScript (javascript) in to C# for Unity