- Home /
The question is answered, right answer was accepted
problem with rotate camera around player
i want to make camera rotate around player, but i can't edit the rotation or location, this my script. get touch :
public Vector2 touchDist, pointerOld;
protected int pointerId;
public bool pressed;
private void Update()
{
if (pressed)
{
if (pointerId >= 0 && pointerId < Input.touches.Length)
{
touchDist = Input.touches[pointerId].position - pointerOld;
pointerOld = Input.touches[pointerId].position;
}
else
{
touchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - pointerOld;
pointerOld = Input.mousePosition;
}
}
else
{
touchDist = new Vector2();
}
}
public void OnPointerDown(PointerEventData eventData)
{
pressed = true;
pointerId = eventData.pointerId;
pointerOld = eventData.position;
}
public void OnPointerUp(PointerEventData eventData)
{
pressed = false;
}
and this my script to rotate camera around player :
public Camera mainCamera1;
public FixedTouchField touchField;
public float cameraDistancePlayer = 11, cameraPosYMax = 11, cameraPosYMin = 1, cameraAngleY, cameraPosY, cameraPosHSpeed = 0.1f, cameraPosYSpeed = 0.03f;
private void Start()
{
cameraAngleY = -180;
cameraPosY = 6.279997f;
}
private void Update()
{
cameraAngleY += touchField.touchDist.x * cameraPosHSpeed;
cameraPosY = Mathf.Clamp(cameraPosY - touchField.touchDist.y * cameraPosYSpeed, cameraPosYMin, cameraPosYMax);
mainCamera1.transform.position = transform.position + Quaternion.AngleAxis(cameraAngleY, Vector3.up) * new Vector3(0, cameraPosY, cameraDistancePlayer);
mainCamera1.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2 - Camera.main.transform.position, Vector3.up);
}
with this script i can rotate camera around player, but i can't edit rotation and location, is there a way to rotate the camera around the player but I can edit the location and rotation of the camera ??, maybe it can use vector or something ??
Are you trying to make the camera move as the player swipes? If so, try something like this:
float angle = 0;
float yPosition = 0;
float[] yClamps = new float[2] { y$$anonymous$$in, y$$anonymous$$ax }; // the $$anonymous$$ and max height values for your camera
float distance; // the distance you want the camera to be from the target
float[] sensitivity = new float[2] { xSensitivity, ySensitivity }; // the sensitivity to apply to the players touches
Camera camera; // the camera
Vector3 targetPosition; // the position in world space of your target
void Update
{
// get the x distance between old and new touch and add it to the rotation angle
angle += (camera.ScreenToViewportPoint(oldTouch).x - camera.ScreenToViewportPoint(newTouch).x) * sensitivity[0];
// wrap the angle between 0 and 360
if (angle > 360) angle -= 360;
else if (angle < 0) angle += 360
// get the y distance between old and new touch and add it to the y position
yPosition += (camera.ScreenToViewportPoint(oldTouch).y - camera.ScreenToViewportPoint(newTouch).y) * sensitivity[1];
// clamp the y position
yPosition = $$anonymous$$athf.Clamp(yPosition, yClamps[0], yClamps[1]);
// use the angle to calculate a point using cosine and sine, then add the height value to it, then normalize it to the desired distance
camera.transform.position = new Vector3($$anonymous$$athf.Cos(angle * $$anonymous$$athf.Deg2Rad), yPosition, $$anonymous$$athf.Sin(angle * $$anonymous$$athf.Deg2Rad)).normalized * distance;
// make the camera look at the target
camera.transform.LookAt(targetPosition);
}
Follow this Question
Related Questions
How to Rotate Camera around GameObject (Player) 0 Answers
Rotate camera and player to face object 0 Answers
Mouse control rotate around player 0 Answers
SideScroller Rotate 1 Answer
Camera move belong a certain path 1 Answer