Rotate Player around the nearest object

Updated on September 4, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
9 on August 30, 2018

I need to have the car rotate around the nearest cone – I’m currently finding the nearest cone as you can see in the .gif 

Also, I believe I need to use the method Transform.RotateAround– Just unsure of the best approach 

My goal is to have the car moving in one direction and when I press the space bar, the car rotates around the closest cone until I let go of the space bar

** Note ** Currently I’m using WASD to move the player just to test if I can grab the nearest cone

Any thoughts?

  • Liked by
Reply
0 on August 30, 2018

It kinda depends on HOW you want it to orbit the node.

RotateAround will work, but it will ignore physics, and you’ll need to change the cars facing directional manually. If you’re using rigidbody for your WASD movement, maybe just stick with that.

You already have the node position, and we have the car’s position and direction. The direction from the car to the node is (node position – car position). Once you have that, you can just keep pushing the car forward however you already are, and then rotate the car like it’s trying to look at the node directly (this can also be done the same way you turn the car manually). With out knowing exactly how the movement is being handled, I can’t give you the exact functions or math you need for the rotation.

However, anyway you do it will involve just trying to get the car to look at the node, while still moving forward at the same time. If you can sync its turning speed to its forward speed, it should orbit the node in a near perfect circle.

  • Liked by
Reply
Cancel
7 on August 30, 2018

@Mouledoux Thank you for the insight – I have the car only turning in one direction only and at a very large arc and very slow turn

if (Input.GetKey(KeyCode.Space))
{
 Vector3 vectorToTarget = closestCone.transform.position - transform.position;
 float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
 Quaternion qt = Quaternion.AngleAxis(angle, Vector3.forward);
 transform.rotation = Quaternion.RotateTowards(transform.rotation, qt, Time.deltaTime * rotationSpeed);
}
Wise
on August 30, 2018

Your code looks good.

 

For the single turning direction: you could do a quick check of the angle between the direction to the node and the right side of the car. If it’s greater than 90 (absolute value) the turn to the right. Otherwise, it must be more to the left, so just turn left instead.

 

The large arc and slow turn are the same issue, so try adjusting your rotationSpeed variable. You don’t necessarily need the speed to match the forward speed, you just need to find the sweet spot. The speed of the car going forward AND the turning speed will both affect how large the turning radius is.

 

One thing you may want to think about is modifying the speed based on how far from the node you are. This can make it so over time, the car could gradually get closer to the node until some predefined limit is reached and the car can chill at that distance.

on August 31, 2018

Thank you, I’ll see what I can make happen

 

Vector3 vectorToTarget = closestCone.transform.position - transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
Quaternion qt = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qt, Time.deltaTime * rotationSpeed);
 
if (angle < 90.0f) {
 Debug.Log("RIGHT: " + angle);
} else {
 Debug.Log("LEFT: " + angle);
}

 

Why is the console printing two different angles, what am I missing?

 

 

RIGHT: -32.6097
UnityEngine.Debug:Log(Object)
 
RIGHT: -92.57905
UnityEngine.Debug:Log(Object)

 

 

Wise
on August 31, 2018

Dont use atan here.
Get the angle with this:

float angle = Mathf.Abs(Vector3.Angle(transform.right, vectorToTarget));

Sorry for formatting, I’m on my phone rn.

on August 31, 2018

No worries, I appreciate the input – I’ll give it a go

on September 2, 2018

a little update on the progress – need to improve the turning and also render a line between the two points – Tap to start and tap (and hold) to turn

Wise
on September 2, 2018

To render a line, try using the LineRenderer and update its points when you update the cars position. For turning speed, just keep messing with the speed variable you already have. Maybe have it increases as long as you’re touching the screen before resetting to its original.

on September 4, 2018

added some tire marks and got the line to render

Show more replies
  • Liked by
Reply
Cancel