- Home /
Slerp/Rotational Problem Query
First, I am trying to design a simple space-sim, which doesn't seem to be so simple in fact. Second, let me show you a picture.
Fig(A): It is a my gameplay view. Fig(B): Scene view with World/Local positioning of my gameobject. Fig(C): Let's skip this for now as I will explain it in due time. Chart: A Hierarchy chart where 4 gameobjects are child of one single gameobject.
Now, onto my problem:
I have this code:
public class mouseTurn : MonoBehaviour {
public float speedmultiplier = 0.005f;
void FixedUpdate(){
float xvalue = Input.mousePosition.x;
int screenwidth = Screen.width;
float xmidpoint = screenwidth/2;
constantForce.relativeTorque = new Vector3(0f,(xvalue-xmidpoint)*speedmultiplier,0f);
}
}
This script is attached to my "Main GameObject"(which also has a RigidBody/ConstantForce attached to it), which enables the entire hierarchy to rotate, which is working absolutely fine[I ommited a lot of code(ex. Y-Axis rotation) and other things to make my question precise and onto point].
What I want to accomplish is that when ever I rotate my object, the child object "Capsule" should rotate just a small amount, to the right/left of my camera, in Time t as to show the effect of capsule turn(For relevance: check Freelancer/Black Prophecy gameplay)[As shown in Fig(C)].
I tried using Quaternion Slerp but could not figure out a way to achieve said goal. While using quaternion slerp, my "Capsule" would not rotate along with camera.
If you see my previous post, you can see that I was able to achieve a fluid rotation using "ConstantForce", which you can also see in the give code. Since, I have optimized the said code a lot(i.e why I have only posted a snippet of it).
So if any Unity Gurus are out there who can help me with my dilemma, I would be very thankful(Atleast show me a path to a solution).
Answer by syclamoth · Feb 16, 2012 at 05:53 AM
Basically, instead of making the camera a direct child of the main movement manager, make it seperate with a script that goes something like this-
public Transform followThis; // Put your capsule here
public float smoothingFactor = 3; // Make this higher to make the camera stick more closely to the target.
private Vector3 posOffset;
void Start()
{
posOffset = transform.position - followThis.position;
}
void LateUpdate()
{
transform.position = followThis.position + posOffset;
transform.rotation = Quaternion.Slerp(transform.rotation, followThis.rotation, Time.deltaTime * smoothingFactor);
}
You may, in fact, wish to put this script on a 'parent' object, that sits on top of the capsule itself, with the actual camera a child of that object offset a fair way.
Thank you for the code snippet. I tried this code in every other way I could think of, but the results were not what I was trying to go for. Pardon my rudeness but let me clarify a bit more.
What I was going for: The camera to follow "Capsule/$$anonymous$$ain Gameobject"'s throughout their rotation, with, capsule rotating a bit to the left/right(depending on the axis of rotation), which exposes capsule's left/right side a bit to the camera, and stay there throughout the rotation in said axis(and this should happen like a form of small animation where capsule seems to rotate a bit to the left/right of the camera view that is why I brought up slerp in above post, because I thought that is how one could achieve said task).
What above code did for me: The camera rotates independently of "$$anonymous$$ain Gameobject/Capsule", which makes capsule invisible after a certain degree of rotation(even with passing of "$$anonymous$$ain Gameobjects/Capsule"'s Transform, and, does not matter whether "Camera" is a child or independent of "$$anonymous$$ain GameObject/Capsule").
I will tinker some more with the code above.
P.S. A/B means Either/Or in my comment.
$$anonymous$$ake the camera a child of a 'pivot' object, which occupies the same space as the capsule. Put the interpolation script on the pivot, not on the camera itself.