There is a camera behavior that I want, but I can’t seem to get it to work. Here’s the idea.
The game style is similar to the classic side-scrolling beat-em-ups, like TMNT arcade, Streets of Rage, etc. I have the camera working fine moving from left to right through a level. However, we want the player to be able to walk along, hit a corner, and it rotates around that corner. An overhead view of the idea is below:
The green highlighting the borders of the corner represent invisible triggers I have been using to determine when a player enters the corner.
When the player enters the corner, I want the rotation of the camera (and also the player) to be based on how far around the corner the player is. An overhead view of this idea is below:
Red dot: the player
Black dot: mostly a reference; focal point and pivot point
Green bars: same as the previous image
When the player hits the trigger, the camera and player pivots from its location by a total of 90 degrees counter-clockwise.
I’ve been playing with this for days, but haven’t really gotten anywhere. I was wondering if any has done something similar and could guide me in a direction to try.
This was probably too vague, but it’s the best way I could think of to explain it. If you have any specific questions, I will do my best to answer.
Thank you very much for taking the time to read this!
barinelg
is the camera a child of the player or do you move them “independently”? does the player rotate smoothly around the corner or does it turn instantly? have you started making any code yet? if so, this would be easier to work with instead of making a completely new script for you
I’m new and haven’t done something like this, but I have an idea:
When the player fires the first trigger it receives the position of the pivot (the edge of the house (an empty GameObject referenced by the trigger)) and switches to something like PlayerMovement.RotationMode. While in said mode the player object makes sure that it is always rotated perpendicular to the pivot. When the player moves the object to the left or right it is just moved forwards/backwards. That way the player object would naturally rotate around the pivot as if it had roped it with a lasso.
Once it exits (to the right direction) the second trigger, movement mode switches back to PlayerMovement.Normal.
As long as the camera is scripted to always show the player from the side (or is simply a child), I think it should rotate automatically.
To dbp;
-What I had going was:
Player (empty gameobject to act like a folder)
PlayerModel
Camera
The camera script has a gameobject variable that is tied to the PlayerModel object, and updates it position off that. Having the camera be a child to the model would definitely make this a lot easier.
-I am aiming for smooth rotation around the corners.
-I have some code, but nothing I tried with it worked, so was going to see what other ideas people had and try those and show. Aiming for conceptual first.
To newbrand:
Having the camera be a child to the player model should definitely make it follow the player far more easily, even on a rotation.
I’ve been calculating the angle between the trigger wall and the player. The idea is that the 2 triggers are 90 degrees. The player will be between 0 and 90 degrees as he goes around the corner. Here’s what I have been doing to calculate the angle:
if(isInCorner) {
//triggerRefPoint is a point that is located where the 2 green bars "meet"
//in the diagram. I move all vectors back to the origin for the calculations
Vector3 player = transform.position - triggerRefPoint;
//The two vectors used to calculate the angle are put on the same Y axis
//(which is 0), so the Z and X coordinates are mainly used to calculate
//the angle. The idea is to make it like a 2D triangle.
player.y = 0.0f;
//triggerV1 is an vector along the trigger
angle = Vector3.Angle(triggerV1, player);
//We want to be sure that we aren't constantly going to do a rotate call
//with the angle as, say 40 degrees. This would just make the player
//object continuously circle. I store the value of the previous angle and
//compare it to the current angle. If they are the same, the player hasn't
//moved, so no rotation is required.
if(prevAngle != angle) {
toRotate = angle - prevAngle;
prevAngle = angle;
}
else {
toRotate = 0.0f;
}
}
When you say “rotated perpendicular to the pivot”, is this what you had in mind? Or did you have another mathematical process in mind? I think you get exactly what I’m looking for.
I modified your image to visualize the process. The goal is to rotate the player object so that it’s x-axis is pointing away from the pivot as long as it is between the two triggers:
Here’s a simple script that demonstrates how someone who forgot almost everything about trigonometry(me) could do this:
// will rotate the object it is attached to
// around pivot as it moves (y-axis only)
var movementSpeed: float = 5.0;
var pivot: Transform;
var isInCorner: boolean = true;
private var rightAngle: float = 90.0;
function Update ()
{
var movement: float = Input.GetAxis("Horizontal") * Time.deltaTime
* movementSpeed;
transform.Translate(0, 0, movement);
if (isInCorner)
{
var relativePosition: Vector3 = pivot.position - transform.position;
var lookRotation: Quaternion =
Quaternion.LookRotation(relativePosition);
transform.rotation.eulerAngles.y = lookRotation.eulerAngles.y - rightAngle;
}
}
Please note that the only thing it changes is the y-rotation of the player object. I’d prefer having an empty game object as pivot instead of calculating the intersection point between the triggers, as it seems to be a bit more flexible.
Thanks for the reply! I apologize for the lateness. My laptop went a bit nuts and I’m out of town…
I was able to give it a try. It almost works perfectly, outside of a few things I need to play with to see if I can get everything working 100%. If there is an issue, I’ll be sure to post another reply.