-4
\$\begingroup\$

I am trying to move a cube to the right in my scene with this piece of code, but it does not work. What is the problem?

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

Vector3 endPos;

void Update()
{
 if(Input.GetButtonDown("right") && gameObject.transform.position == endPos)
 {
     endPos = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
 }
 }    
 }
\$\endgroup\$
  • \$\begingroup\$ What version of Unity? \$\endgroup\$ – Nick Savage Aug 16 '15 at 20:41
  • \$\begingroup\$ Unity Version: Unity 5.1.2f1 \$\endgroup\$ – That's me. Aug 16 '15 at 20:45
  • \$\begingroup\$ Do you have a RigidBody attached to your object? \$\endgroup\$ – Nick Savage Aug 16 '15 at 20:56
  • \$\begingroup\$ Yes, I do have a RigiBody attached. \$\endgroup\$ – That's me. Aug 16 '15 at 21:00
  • \$\begingroup\$ For starters, one problem is the gameObject.transform.position == endPos part of the condition. Since endPos isn't set, that condition won't be true. What exactly are you attempting to accomplish anyway? You don't explain what you're trying to do, aside from vaguely saying "move a cube to the right". \$\endgroup\$ – jhocking Aug 16 '15 at 21:49
1
\$\begingroup\$

There are multiple ways to move your object in the game world. You can set the transform manually (method 1), translate the object from it's local coordinates (method 2), or add a force onto your object's rigidbody (method 3).

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

    private Rigidbody rb;
    private float thrust;

    void Start() {

        rb = GetComponent<Rigidbody>();
        rb.mass = 10;
        thrust = 5000;

    }

    void Update () {

        if(Input.GetKeyDown(KeyCode.Space)) {

            //You can use any of these

            //method 1
            transform.position += Vector3.up;

            //method 2
            transform.Translate(Vector3.up);

            //method 3
            rb.AddForce(Vector3.up * thrust);

        }

    }
}

Use only one of the options to make your object "jump".

\$\endgroup\$
  • \$\begingroup\$ Excellent answer. I found why previous codes were not working! The cube was animated. How can I move the object while keeping the animation? I will accept the answer once I solve this problem :) \$\endgroup\$ – That's me. Aug 16 '15 at 22:40
  • 1
    \$\begingroup\$ That's an entirely different question but treat the animation as a child of a game object, then move the game object. \$\endgroup\$ – Nick Savage Aug 16 '15 at 22:49
  • \$\begingroup\$ How can I contact you in case I have more questions? \$\endgroup\$ – That's me. Aug 16 '15 at 23:08
  • 1
    \$\begingroup\$ I'm flattered you'd ask but I really must advise just asking further questions here on the GameDev Stack Exchange. You have a large audience of developers that can give you a lot of different insights that I wouldn't be able to do by myself. Thanks for the accepted answer. \$\endgroup\$ – Nick Savage Aug 16 '15 at 23:46
  • 1
    \$\begingroup\$ Create a new question \$\endgroup\$ – Nick Savage Aug 17 '15 at 20:06

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.