- Home /
Camera 360 not following parent
This Camera script zooms and orbits around Player with mouse. The problem is that when i rotate my player the camera stays still.
[CODE]
using UnityEngine; using System.Collections;
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class DragMouseOrbit : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
[/CODE]
Answer by prototype7 · Mar 06, 2013 at 03:02 AM
Try move the last bracket of Input.GetMouseButton(0)
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
}
just make sure you attach the script in the Main Camera and drag your sphere to target in the inspector
Answer by cryptoshaman · Mar 06, 2013 at 04:52 AM
Hi proto
I implemented your change but did not get the result I was looking for here is an image to explain.
that's weird, send me your full code, including the code to make sphere rotate. Let me see if i can help to debug it.
Here is the full code after editing; The original is the ones posted above ;
public class Drag$$anonymous$$ouseOrbit : $$anonymous$$onoBehaviour { public Transform target; public float distance = 5.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f;
public float y$$anonymous$$inLimit = -20f;
public float y$$anonymous$$axLimit = 80f;
public float distance$$anonymous$$in = .5f;
public float distance$$anonymous$$ax = 15f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
if (Input.Get$$anonymous$$ouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("$$anonymous$$ouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("$$anonymous$$ouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
distance = $$anonymous$$athf.Clamp(distance - Input.GetAxis("$$anonymous$$ouse ScrollWheel") * 5, distance$$anonymous$$in, distance$$anonymous$$ax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = target.rotation;
transform.position = position;
velocityX = $$anonymous$$athf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = $$anonymous$$athf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
public static float ClampAngle(float angle, float $$anonymous$$, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return $$anonymous$$athf.Clamp(angle, $$anonymous$$, max);
}
}
This is my final answer,
in the void Start() initialize your target rotation, in this case the sphere and think about axis, x rotation is y axis, and yrotation is x axis. And your Quaternion rotation is incorrect too.
i simplify your code according to your problem
void Start()
{
if (settings.vertical_mirror) mirror_y = 1; // vertical mirror
if (!settings.showCursor) Screen.showCursor = false;
// initialize your target rotation, in this case the sphere
Vector3 angles = transform.eulerAngles;
// think about axis, x is y, and y is x
rotationXAxis = angles.y;
rotationYAxis = angles.x;
}
void LateUpdate()
{
if (Input.Get$$anonymous$$ouseButton(0))
{
if (target)
{
//
rotationXAxis = rotationXAxis + Input.GetAxis("$$anonymous$$ouse X") * 100 * 0.05f;
rotationYAxis = rotationYAxis + Input.GetAxis("$$anonymous$$ouse Y") * 100 * 0.05f * mirror_y;
Quaternion rotation = Quaternion.Euler(rotationYAxis, rotationXAxis, 0);
Vector3 position = rotation * new Vector3(0, 0, -10) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
}
$$anonymous$$ake sure that when you're done you give @prototype7 credit where credit is due by accept his answer!
Your answer
Follow this Question
Related Questions
1st person Camera rotates suddenly in another direction when unparenting from a platform 1 Answer
Parenting camera to object but keeping rotation? 1 Answer
Child affecting parent rigidbody? 1 Answer
Quaternion - Euler Angles, weird variable swapping when converting 2 Answers
i want the camera stop follwing my player only on y axis when he jumps... 4 Answers