Unity transform an object to a local Vector3 not global - c#

So I want to instanstiate an object and update its position from what where I am spawning it. I am spawning this object on the parets + 10x position.
Instantiate(gameObject, transform.position + new Vector3(10, 0, 0), Quaternion.identity);
When I do this, it nearly does what I want it to, however it will spawn the object and give it 10x on the global position not local, where it was spawned from. After looking online, I tried:
transform.localposition
However that gave me the error of "CS1061: 'Transform' does not contain a definition for 'localpositon'"
Would anyone know what I am missing when it comes to instanstiating something on a local Vector3?
Edit* Thinking of the terminology, I guess the result that I am expecting is for the object I am creating to be pivoted around a certain point when spawned in, not just rotated and position on the Vector3 coordinates.
This point being the spawn location in the world, which is just parent world location + child location.

When a name does not work try to use auto complete (visual studio) or verify with the Unity docs that it is spelled correctly and that it exists.
The field you seek is called localPosition (P is capital).

Related

Setting the rotation to a mix of localRotation and world Rotation in Unity 5 (C#)

I'm trying to set a child gameObject equal a mix of the localRotation of the root and 0's of the world.
(This gameObject is a gun of sorts that the player character holds, like a fireflower, and out of the face of this object fireballs are released)
Essentially I want this item to always face forwards (so it faces towards where my character is facing). When I do:
this.transform.rotation = Quaternion.Euler(0,0,0);
the X and Z are correct, but the Y value always stays facing the same forward direction no matter where the character rotates.
I've messed around and found that
this.transform.localRotation = Quaternion.Euler (270, 180, 90);
works slightly, but it is off by an amount that would require me to do a lot of trial and error with and I would rather work with non hardcoded values.
In the end I want to have a localRotation equal to (0 of World, parentRotationY, 0 of World), but I can't get it to work.
So is there any way to mix a localRotation and a world rotation?
Also if I'm not providing enough information just let me know.

How to add random x, 0, z movement to GameObjects on Terrain?

I'm inexperienced with UnityScript and C#, and a solution I can study would be greatly appreciated.
The behavior I want is for Pickups to move in random directions on a terrain, changing direction if they encounter a rigid body or static object.
I tried the script "Wander.cs" available here:
http://wiki.unity3d.com/index.php/Wander
However, when played, the script turns the game object on end, and I cannot correct this. Also, objects tend to pool in corners instead of heading away.
There are two possibilities as far as I can understand your query.
I didn't try but hope it helps.
1- With help of parametric equation of circle i.e.
x=v.t.cos(theta)
y=v.t.sin(theta)
In you case:
x=v.t.cos(theta) + xDistance
z=v.t.sin(theta) + zDistance
where xDistance and zDistance are simple initial position value of your body. These value will not be changed after starting movement (during movement until collision occur).
In Update() apply these equations to your body with constant v and theta and increase t (t++ or t+= 0.1f) by time. When body collides, just retain the current translate (position) value. And start movement again, this time change the value of xDistance and zDistance with current value but only once.
2- With help of iTween. Its a free animation script available at Asset Store
You can use iTween.MoveTo to move body to any given direction (You can assign a random direction as well.). And stop and restart movement upon collision.

Making a rotating object rotate with a parent object

I have a game object which swings back and forth along one of its axis, this is attached to a parent gameObject which moves around a scene. However, when the parent object rotates the child object does not rotate with it.
I expect this is something to do with local vs global space.. however Im at a loss...
This is the code which manages the sweep.
// Increment a timer
timer += Time.deltaTime;
// Swing the object back and forth based on the rotationLimit and the rotationSpeed
currentRotation = Mathf.PingPong (timer * rotationSpeed, rotationLimit * 2f) - rotationLimit;
// Create a temporary variable to store the new rotation
Quaternion rotation = Quaternion.Euler (transform.rotation.x, transform.rotation.y + currentRotation, transform.rotation.z);
// Set the rotation to the temp var
transform.rotation = rotation;
I considered grabbing the parents rotation and using that instead of the transform.rotation. However, ideally this same script needs to work on a variety of objects, some of which don't have parents. Can anyone advise where I am going wrong.
From your tags i'm guessing you use Unity. As far as I know, unity supports the concept of a 'SceneGraph'. This allows you to build a tree of game objects, where the children follow the transformation of their parent.
See this link: http://docs.unity3d.com/ScriptReference/Transform-parent.html
ALTERNATIVE - If you want to do this manually:
You should be carefull when manually combining parent/child transformations. The easiest and probably best way to do this is to simply combine the parent and child transformations:
ChildWorldTransform = ChildLocalTransform * ParentWorldTransform
ChildLocalTransform corresponds to your currentRotation, ParentWorldTransform to the transformation of your parent object. Note that you should multiply the entire transformation as a whole, not just the rotation part!
I also suggest that you do a google for 'scenegraph'

How to apply force to an object in a particular direction in Unity

I'm using unity to develop my game. I've made a custom swipe gesture by calculating the startPosition and the end Position of the touch. I got the vector direction by subtracting the two positions and debugged it successfully.
But when it comes to applying force to my game object I'm not getting any success after trying a lot.
here's what I'm doing:
swipeDirection = new Vector3(endPos.x - startPos.x,0f, endPos.z - startPos.z);
swipeDirection.Normalize();
Debug.Log("The direction is "+ swipeDirection);
ball.rigidbody.AddForce(swipeDirection * 5);
where ball is just a GameObject. Whenever I run it on my iPhone, the game just gets stuck giving a EXC_BAD_ACCESS code after the first swipe.
So I just tested your code. And it works. This issue you may be having is that those number are pretty small most likely, and you need a LOT of force to move it noticeably. Try changing your 5 to a 1000 or something big so you can definitely see the change. Here is my code, just added to a ball with rigidbody.
void Start ()
{
Vector3 dir = new Vector3 (100f, 0f, 0f);
dir.Normalize ();
this.gameObject.rigidbody.AddForce (dir * 100);
}
almost identical. you just need to make the multiplier big enough to see.
good luck.

using MDX Sprite.Draw2D() and the positional problems i have encountered

I'm trying to use the Sprite Class in Microsoft.DirectX.Direct3D to draw some sprites to my device.
GameObject go = gameDevice.Objects[x];
SpriteDraw.Draw2D(go.ObjectTexture,
go.CenterPoint,
go.DegreeToRadian(go.Rotation),
go.Position,
Color.White);
GameObject is a class i wrote, in which all the basic information required of a game object is stored (like graphics, current game position, rotation, etc)
My dice with Sprite.Draw2D is the Position parameter (satisfied here with go.Position)
if i pass go.Position, the sprite draws at 0,0 regardless of the Position value of the Object.
i tested hard-coding in "new Point(100, 100)" and all objects drew at 100,100.
I cant figure out why the variable doesnt correctly satisfy the parameter.
I've done some googling, and many people have said MDX's Sprite.Draw2D is buggy and unstable, but i didnt find a solution.
Thus i call upon Stack Overflow to hopefully shed some light on this problem!
Fixed
Yes sprite.Draw2D some time gives problem. Have u tried sprite.Draw its working fine for me.
Here is the sample for Sprite.Draw.
GameObject go = gameDevice.Objects[x];
SpriteDraw.Draw2D(go.ObjectTexture,
new Vector3(go.CenterPoint.X,go.CenterPoint.Y,O),
new Vector3(go.Position.X,go.Position.Y,O),
Color.White); and for rotation u can use matrix transform of sprite.

Categories