# Transform Vector  from Camera Space to Object Space

**URL:** <https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064>\
**Category:** Camera\
**Tags:** Scripting\
**Created:** [February 21, 2013, 12:26am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064 "2013-02-21T00:26:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Localguy65](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Localguy65](https://discussions.unity.com/u/Localguy65)\
**Post date:** [February 21, 2013, 12:26am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/1 "2013-02-21T00:26:52Z")

</div>

Is there a command or does anyone how to take a vector from one object (Camera) and transform it into another object local space?

I used to use a command set like this.

TFormvector( X,Y,X, From Object, To Object)

X1=TFormedX()  
Y1=TFormedY()  
Z1=TFormedZ()

Thanks for any help,  
Eric

---

<div class="post-metadata">

**Author:** ![JamesLeeNZ](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jamesleenz/32/233694_2.png) [@JamesLeeNZ](https://discussions.unity.com/u/JamesLeeNZ)\
**Post date:** [February 21, 2013, 12:30am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/2 "2013-02-21T00:30:17Z")

</div>

A quick google will show the results you need… you should try google some time… its quite useful

[https://www.google.co.nz/search?q=Unity+world+to+local&aq=f&oq=Unity+world+to+local&sourceid=chrome&ie=UTF-8](https://www.google.co.nz/search?q=Unity+world+to+local&aq=f&oq=Unity+world+to+local&sourceid=chrome&ie=UTF-8)

the first link, will no surprise, take you to the unity docs…

> **[Unity - Scripting API: Transform](https://docs.unity3d.com/ScriptReference/Transform.html)**

scanning down the page, you will notice functions for transforming world to local space.

---

<div class="post-metadata">

**Author:** ![Localguy65](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Localguy65](https://discussions.unity.com/u/Localguy65)\
**Post date:** [February 21, 2013, 12:55am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/3 "2013-02-21T00:55:15Z")

</div>

I did search google but couldn’t form my question properly. My First post and I get slammed. ☹

My Question is how to go from an object transform to another Objects Transform. or from Camera Space to another GameObjects space.

What I’m trying to do is control a hovercraft. I’m tipping it forward and backwards, front to back and I’m creating thrust from below.

I’m controlling the ship from a joystick(Game pad).

Everything seems good until my camera angle changes.

This is a little of my crazy code.

```auto
	void Update () 
	{
		 
		ZZ=Input.GetAxis("Horizontal");
		XX=-Input.GetAxis("Vertical");	
		Thrust=(Input.GetAxis("Thruster")*5.00f);
		cameraRelative= Cam.transform.InverseTransformDirection(ZZ, 0, XX);
	
		
	}
	
	void FixedUpdate ()
	{
		
		 	
		rigidbody.AddRelativeForce(Vector3.up*Thrust,ForceMode.Impulse);
		
		rigidbody.AddForceAtPosition( new Vector3(cameraRelative.x*ControlThrust,cameraRelative.y*ControlThrust,-cameraRelative.z*ControlThrust),transform.position + new Vector3(0.0f,0.625f,0.0f),ForceMode.Force);
		rigidbody.AddForceAtPosition( new Vector3(-cameraRelative.x*ControlThrust,cameraRelative.y*ControlThrust,cameraRelative.z*ControlThrust),transform.position + new Vector3(0.0f,-0.625f,0.0f),ForceMode.Force);
		
	}

```

---

<div class="post-metadata">

**Author:** ![JamesLeeNZ](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jamesleenz/32/233694_2.png) [@JamesLeeNZ](https://discussions.unity.com/u/JamesLeeNZ)\
**Post date:** [February 21, 2013, 2:14am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/4 "2013-02-21T02:14:08Z")

</div>

yes I was perhaps a little harsh…

It might be easier if you don’t relate the movement to the camera. Is there a particular reason you are doing it this way?

---

<div class="post-metadata">

**Author:** ![JamesLeeNZ](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jamesleenz/32/233694_2.png) [@JamesLeeNZ](https://discussions.unity.com/u/JamesLeeNZ)\
**Post date:** [February 21, 2013, 2:43am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/5 "2013-02-21T02:43:49Z")

</div>

why do you have multiple accounts?

also, controlling from a joystick is a terrible answer.

Your vehicle controller should be completely separated from your camera controller

---

<div class="post-metadata">

**Author:** ![Localguy65](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@Localguy65](https://discussions.unity.com/u/Localguy65)\
**Post date:** [February 21, 2013, 2:45am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/6 "2013-02-21T02:45:08Z")

</div>

The camera follows the ship around. In a fixed distance orbit of sorts… As the ship passes the camera the camera rotate to face the ship and also maintains a fixed distance. If the ship flies toward the camera the camera moves to keep a fixed distance a rotates in the orbit

The camera follow routine is not started but I’ve written it in other engines.

If my ship starts to rotate on the y axis my controls get all screwed up.

Think of a rocket and I want to tip the rocket along the X and Z Axis. The Game pad forward should translate from my current view and tip the Rocket away from the camera. Game Pad Back should tip the Rocket towards the camera…

The Rocket is free to spin on it’s Y Axis so the forces I apply have to be transformed from camera space to Rocket Space.

It’s really hard to type what I’m trying to do I hope this makes a little sense.

Here is a video of something I was working on last year.

[http://www.youtube.com/watch?v=OrrbvBKZkKk](http://www.youtube.com/watch?v=OrrbvBKZkKk)

---

<div class="post-metadata">

**Author:** ![JamesLeeNZ](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jamesleenz/32/233694_2.png) [@JamesLeeNZ](https://discussions.unity.com/u/JamesLeeNZ)\
**Post date:** [February 21, 2013, 2:49am UTC](https://discussions.unity.com/t/transform-vector-from-camera-space-to-object-space/498064/7 "2013-02-21T02:49:40Z")

</div>

Now youre just not making any sense at all…

“The camera follows the ship around. In a fixed distance orbit of sorts…”

totally contradicts the second part…

“As the ship passes the camera the camera rotate to face the ship and also maintains a fixed distance.”

how exactly does a ship pass through a camera that is following the ship?

I would use the standard asset camera follow/orbit script for now, and worry about getting the hovercraft or whatever it is to drive correctly. All camera movement should be in your camera controller… it will make your life a lot simpler
