Search Unity

Vector Script

Discussion in 'Scripting' started by Pinkesh, Apr 3, 2012.

  1. Pinkesh

    Pinkesh

    Joined:
    Apr 3, 2012
    Posts:
    6
    Hello Everyone,

    I am trying to develop vectors with magnitude and direction. I want to attache those vectors to moving body with variable v (velocity), acceleration also force vector. Can i do that ? . If yes then please can you guide me. I am working on research project.
    I want arrows like in the figure. This are friction and normal reaction vectors. How can i do in Unity ?

    Thank You
    Pinkesh
     

    Attached Files:

  2. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    Like anything when it comes to programming.... there are many ways, it just depends on what you are after.

    One way is to have a 3D model of an arrow that you scale to size its length. Of course that might cause unwanted distortion on the arrow head.

    The other is to generate the mesh of the 3D arrow parametrically. If you are not up to that task, look in the Asset Store. There are a few parametric primitives assets that you can buy cheaply.

    You'll still have to understand how to code this all up. But that's a start.
     
  3. Pinkesh

    Pinkesh

    Joined:
    Apr 3, 2012
    Posts:
    6
    Thanks David. According to you i can buy those arrows as a component asset and i can attach to the car is that correct ?
    I want to show visually those contact forces (can be calculated based on physics formula) on tires besides that i also need to show velocity and acceleration vectors ( can be calculated based on physics formulas ). How can it be possible to render these kind of arrows on screen ?.

    Please give me little more guidance. It will be appreciated.

    Thank You
    Pinkesh
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Pinkeshkumar: Am I remembering correctly that you want these arrows to show up in your Build, not in the Scene View of the Editor? For an In-Editor solution, I would look at the Debug class (http://unity3d.com/support/documentation/ScriptReference/Debug.html) and look at draw ray and/or draw line. This won't give the arrow heads, but you may be able to give you some information in the scene view. For information to be displayed in the Game View, then I would suggest either an approach like nsxdavid was suggesting, where you have an object that is scaled based on the value of velocity vector. This could either be 2d: a sprite plane with an arrow drawn on it and a script attached with to make sure that the sprite faced camera (http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html). Or it could be a 3D object: I would suggest you start off using a primitive cube or cylinder from the GameObject/Create Other menu and later get an arrow once you have the behavior working:



    The script would be something like this:

    Code (csharp):
    1. var myObject : Transform;   //   Note: this is the Transform not the GameObject
    2. var myVector3 : Vector3;
    3. var myScale : float;
    4.  
    5. function Update () {
    6.    myObject.localScale.y = myVector3.y * myScale;
    7. }
    This pseudo-code doesn't address how you get your Vector3, as I'm unclear how you have set up your project. Also, to make this work, the centerpoint of your object needs to be as the base of the object, or you will have to adjust the objects position as well to take into account that the scale is from the center (which would be true if you were using a Unity primitive, as they all have centered center points). And the myScale value gives you control over how big the overall scale/size of the object will be.
     
  5. Pinkesh

    Pinkesh

    Joined:
    Apr 3, 2012
    Posts:
    6
    Thank you all. I will certainly try this. Thank you so much.