Search Unity

Moving object without AddForce

Discussion in 'Getting Started' started by Genkidevelopment, Jan 28, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I have calculated and rotated an object to face the desired direction of travel and want to propel it directly in the direction it is facing...

    I understand if I give the object a rigid body that I can go about this by means of AddForce, or AddRelativeForce.. But really I would rather manipulate the object by means of Transform...

    What is the best way of doing this...

    Thanks...

    Here is the code I have...


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class V2MovePlayerToRabbit : MonoBehaviour {
    5.  public Transform HomeRabbit4; // Linking this game object to the rabbit position
    6.  private float Diffx;
    7.  private float Diffz;
    8.  private float MoveDirection;
    9.  // Update is called once per frame
    10.  void Update () {
    11.  // Diffx and z are variables required to calculate desired angle
    12.  Diffx = gameObject.transform.position.x - HomeRabbit4.position.x;
    13.  Diffz = gameObject.transform.position.z - HomeRabbit4.position.z;
    14.  //Calculate the desired taget angle
    15.  MoveDirection = Mathf.Atan2 (Diffx, Diffz) * 180 / Mathf.PI;
    16.  print ("Move Direction = " + MoveDirection + "degrees");
    17.  
    18.  // Set the player facing it's intended target
    19.  transform.Rotate (0, MoveDirection, 0);
    20.  
    21.  // Now the player is facing its intended target we must
    22.  // add a force along the "FORWARD" axis relative to the new rotation...
    23.  // We can limit the speed this object is allowed to transform at directly with a variable ie "MAX SPEED"
    24.  // We also need to factor in Time.deltaTime so this calculation is not affected by current Frame-rate
    25.  
    26.  
    27.  }
    28.  
    29. }
    30. [code/]
    31.  
    32. Thanks
     
  2. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Haha! Well, I now also see that I am in fact adding that rotation every frame, thus achieving the spinning spiral of AI Doh! :D

    I have sussed the transform.Translate (1, 0, 0 * Time.Deltatime) to get it moving... Only now I need to find a way of making that rotation figure correct :/
     
  3. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751
    You might want to just use transform.LookAt.
    You'll want to move your target to the same Y if you don't want any tilting.
     
  4. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    wouldn't the principal be the same? Just add (transform.forward*velocity*time.delta)?

    edit - more specifically, I think you'd want something like (targetpos-sourcepos).normalize*velocity*time.delta
     
  5. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks you for the advices...

    I researched and found .LookAt late last night... It effectively does the calculation for me... Thus turning a 20 line code into 2...!

    First thing I noticed was the transform slowly pushing my object through the floor... Tilting :D Solved that issue by ensuring the Rabbit (Target) has the same Y plane...

    I would like to know if the 'LookAt' function is more or less costly than what I initially coded.. It seems when calling such function (They look so easy!) But behind the scenes the UNITY engine is doing the hard work, just it cant be seen!

    I also have read that using any '.position' is really bad for performance as effectively the unity engine IS doing a GetComponent.Transform.position without you seeing it - Is this really the case?

    Also I have been made aware of using a cache for these situations, although I don't yet fully understand!

    Thanks again for the support

    Peace...