camera follow gameobject moving in angular path in Unity - c#

I have a gameobject that is moving in angular path like the image
But the camera is not following the player. If I am setting the Y value of the camera equals to the Y value of the player and keeping the x and z constant (0 & -10 respectively). It is showing me straight x movement of the player only (just like the image)
I guess the reason is the same Y value of both camera and player.
I am not sure how to make the camera follow the player that shows the exact movement I have in image 1.
Any help on this will be appreciable.
P.S. - please excuse my drawing skills, I know they are pathetic.....

If you want the camera to follow the player, there is no way to exactly show the movement you have above, since the ball would have to move off the screen. Since the camera position is relative to the ballposition, it will always appear as though the ball is moving horizontally as long as the camera moves at the same y-speed of the ball.
You will have to give the ball some appearance of movement, either by adding texture to the wall, a trail to the ball, a static background, or you could add something like lag to the camera (which will only work if the ball varies in speed):
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, ball.transform.position, .2f); // change .2f for different time lag

Related

How to simulate running on Wall like The Flash in unity

Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)

How to make ball bounce

I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.
It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.
What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.
Someone have any code ideas?
Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a Rigidbody with drag 0.
Also uncheck gravity to get the following result:
Basic Force Script:
After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.
public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);
Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.
For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.
If you'd rather it go in a random direction:
calculate the magnitude of the ball's velocity sqrt(vx^2+vy^2)
find the angle the wall is facing
pick a random angle within 180 degrees of that angle
use trigonometry to get the x/y components of that angle, and multiply by the magnitude.
Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.

Transition Forward is going backward

I am a newbie to unity. In my project, the ball should instantiate in the player
position and it should go randomly 40 to 120 degrees from the player position.
I am using transition.forward. My player is constantly in running. when the player stands the ball is going correctly in front of the player.
but when the player runs the ball is going backward
How can I move towards randomly in front of the player? Like a ball
I am using the below code.
transform.Translate(Vector3.forward*5f );
There is a chance it might be fixed if you spawn the ball in front of the character. Since it works when the player is stationary, it might be conflicting with the collider of player when it is moving.
Also translating will just move the ball, not give it a velocity or anything. You should take a look at https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
So, first translate the ball forward and see if it spawned in the right location.
Then add a force vector to the ball's rigid body in the direction you like.
Something like:
ball.transform.Translate(Vector3.forward*5f);
float thrust = 10;
var rb = ball.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * thrust);

Unity C# Bounce off Wall

So I'm making a 3D pool game. Basically I have a main camera that when you press a button it adds force to the cue ball based on the position of the camera, which works ok.
But whenever the cue ball hits the wall of the table, it just stops. I want it to smoothly bounce off the wall like a real pool cue ball would.
The cue ball is just a basic sphere Game Object. The walls are basic cubes with colliders.
I have tried Vector3.Reflect with no success. It seems to bounce back a tiny bit but then immediately stops.
Any help would be great!
You should create a PhysicMaterial with low or no friction (both dynamic and static), bounciness = 1 and Bounce Combine = Maximum and then apply that PhysicMaterial to the rigidbody of your sphere
You can do one thing.
Store the ball velocity when it collide with the wall, calculate the reflected direction through Vector3.Reflect and give the stored velocity to the ball in reflected direction.
Hope that this will help you...
Best,
Hardik.
To make bounce on wall we need to create physic material and after that we need to change the value of dynamic friction =0.3 and static friction =
0.3 and bounciness=0.8 and frictionCombine choose Average from dropdown and bouncecombine choose Average .
Hence the ball is start bouncing on the wall by using the upper property.

Rotate camera in function of direction of ball

I'm making a maze whit Unity3D where a ball can roll and find the out way. Because the ball can be hide after a wall, I want to rotate the camera to a better position in function of the direction of the ball.
Take this example: the ball is rolling in to you (in the direction of the black arrow). So you can see or beter can't see, is where the ball is rolling to. So the camera must turn to the other side of the ball. If the ball rolls away from you must the camera turn to the original location.
The problem is now, I know how I can replace the camera but not in function of the direction? Can anyone help me with this? I'm just starting with Unity3D. Language behind I use C#.
Here is another situation where it is better to rotate the camera. (up: is scene, below game mode).
You can use this to set the camera position behind the ball based on the velocity and then the rotation in the direction of the ball
Vector3 offset = new Vector3(1,1,0);
transform.position = ball.transform.position - ball.GetComponent<Rigidbody>().velocity / ball.GetComponent<Rigidbody>().velocity.magnitude + offset;
transform.LookAt (ball.transform.position);

Categories

Resources