- Home /
Rotating object's xyz-axis but not its mesh
Hello! I´m making an air balloon simulator containg an air balloon and a first person camera in it.
I want my air balloons mesh to not rotate, but I want the balloon to move towards where the first person camera is pointing and it seem like the xyz-axis depends on the objects rotating. Is there any way to get around this?
My parts: Balloon, camera, part1, part2, part3
So how and where do I put my movement and mouse look scripts, and how do I solve this?
Thankful for an answer!
Answer by aldonaletto · Oct 06, 2012 at 01:17 PM
If I understood correctly what you want, my suggestion is to child the camera to the balloon and attach the MouseLook script to the camera, setting its Axes field to "Mouse X and Y" - this way you can look up/down/right/left with a single MouseLook. To move in the direction the camera is looking at, move the balloon in the camera's forward vector. If you want to control the vertical movement with the camera too, multiply the Y component by some factor between 0 and 1 - this way you can control how much the vertical camera swing affects the vertical movement.
If you need to detect collisions, add a CharacterController to the balloon and use Move; if not, simply use Translate (with Space.World specified). That's a basic movement script (attach it to the balloon):
var speed: float = 5.0; // how much the up/down camera movement influences the balloon vertical movement: var vSensitivity: float = 0.2;
function Update(){ // get the camera's forward direction: var dir = Camera.main.transform.forward; // apply part of the vertical cam direction to the vertical movement: dir.y = vSensitivity; // control forth/back movement with the Vertical axis: var move = dir.normalized Input.GetAxis("Vertical") speed Time.deltaTime; // Translate version: transform.Translate(move, Space.World); // CharacterController version: GetComponent(CharacterController).Move(move); }
NOTE: Remember to keep only one of the movement instructions (Translate or Move), deleting or commenting out the unused one.
Answer by NowhereStudios · Oct 06, 2012 at 06:32 AM
If you are using FPSController, edit your MouseLook script on FPSController and on update function, add "Camera.current." prefix before all transform operations. Your Update function must be look like this:
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = Camera.current.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
Camera.current.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
Camera.current.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
Camera.current.transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
Goodluck ;)
Hi, Nowhere Studios!
When I modify as you told me, I got a nullReferenceException. Any clue of what to do? I tried to instantiate the BalloonCamera but I ended up recieving other errors (I´m quite new to C#, although not to java).
And what does current mean (it´s messy to google it)?
Please help!
Im using the first person controller from the standard assets (modified a bit)! I thought you meant the fps input controller, maybe you meant another script by fps controller?
Hi, I sent the code assu$$anonymous$$g you are using FPSController in Import Assets > FirstPersonController Prefab. If you do, then delete the capsule in it, Add Your baloon on the place of capsule, and finally Disable "Use Gravity" and "Jump" etc. to tune it for yourself. Finally replace the code block i said before.
Okey! But I get a nullReferenceException at $$anonymous$$ouseLookBalloon.Update () (at Assets/Scripts/$$anonymous$$ouseLookBalloon.cs:36). Any idea of how to solve it?