Search Unity

Can't move forward??

Discussion in 'Scripting' started by DubStepMaster, Dec 2, 2014.

  1. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Hey there

    I wrote a script to move an object to a target point. this is my script:

    Code (CSharp):
    1. private void moveToRnd(){
    2.  
    3.         float x = transform.position.x - newPos.x;
    4.         float y = 0f;
    5.         float z = transform.position.z - newPos.z;
    6.  
    7.         Vector3 forward = new Vector3(x,y,z);
    8.         float length = forward.magnitude;
    9.         forward = (1 / length) * forward;
    10.         float curSpeed = speed;
    11.         //motor is a CharacterMotor
    12.         motor.SimpleMove(curSpeed * forward);
    13.         gameObject.transform.LookAt (new Vector3 (newPos.x, transform.position.y, newPos.z));
    14.     }
    the only problem is, that the object moves backwards to my target. (It looks in the right direction)
    every try to turn the direction (*(-1) or change newPos with transform.position) broke my script and the object doesn´t move...

    can anyone help me?

    thank you
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You can subtract vectors :)

    Code (csharp):
    1.  
    2. Vector3 heading = (newPos - transform.position).normalized;
    3. transform.position += heading * speed * Time.deltaTime;
    4.  
     
  3. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Thanks for the reply, but I had a reason for doing that: it doesn't work with this technique.

    I tried this any way and it did: nothing...
    My object does not move

    Why and how do I use the move method from the charactermotor?
    I tried it like the scripting api said, but this doesn't work for me (my character moves backwards in the wrong direction)

    Help please!
     
  4. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    Code (CSharp):
    1. transform.position.x - newPos.x;
    This will calculate the position to the transform.position.x from newPos.x. I assume you're going backwards because you want to calculate the opposite.

    Try:
    Code (CSharp):
    1. float x = newPos.x - transform.position.x;
    2. float y = 0f;
    3. float z = newPos.z - transform.position.z;
     
    DubStepMaster likes this.
  5. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Well, i tried this yesterday and it didn`t worked...
    now i restarted unity and it works...

    thank you for your help
     
  6. DubStepMaster

    DubStepMaster

    Joined:
    Jul 17, 2012
    Posts:
    40
    Okay there is still a problem:

    my code works, until I add this line:

    Code (CSharp):
    1. transform.LookAt(new Vector3(newPos.x,transform.position.y,newPos.z));
    when i add this, my character doesn`t move...

    could anyone explain me this?

    thanks