Transition Forward is going backward - c#

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);

Related

how to stop player position from changing once it is against a wall/static-object?

I am creating a top-down game, Though during testing I found that the enemies weren't going to the player.
I discovered that the coordinates of the player change when it moved, though the coordinates would still change when the player would move against the wall. for example, if I was to move up into a wall, even though the player wasn't moving, its Z coordinate would still be increasing
this is the code that is to move the player:
void FixedUpdate()
{
var dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
transform.Translate(dir * speed * Time.deltaTime);
}
What I then wanted to try was adding a colliding feature to which if it notices that the player's side is touching a wall or static object which meant that it would shut off the ability to continue going in the direction that it is against until it was not touching the wall on the specific side.
though I couldn't get the collision/collider code to work, I found that it wasn't picking up that it was touching an object.
are there any methods to fix this issue?

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.

How can I make my rigid body bullets shoot in a straight line no matter my mouse movement?

I'm making a very simple 3D game in Unity where I have this space shuttle I can move around in space around asteroids and I can shoot when pressing/holding the mouse button. I'm doing this by Instantiating a sphere at the "Emitter" transform.position and then just applying a forward Force to that bullet object.
It all works fine, but the one thing I don't like and also don't know how to fix is how the bullets keep their position when shooting and moving the mouse left-right, instead of keeping a perfectly straight line at all times.
This is how it looks when I'm shooting and moving my camera at the same time:
Screenshot while shooting
Here's a gif for better visualization.
Right now it looks like I'm pissing lasers, which is never good. I tried making the bullet speed a lot faster, but then the bullets become harder and harder to see and it doesn't look as good.
This is the code by which I'm shooting the bullets:
private void Fire()
{
GameObject bullet = Instantiate(laserPrefab);
GameObject bullet2 = Instantiate(laserPrefab);
Physics.IgnoreCollision(bullet.GetComponent<Collider>(), shuttleCollider.GetComponent<Collider>());
Physics.IgnoreCollision(bullet2.GetComponent<Collider>(), shuttleCollider.GetComponent<Collider>());
bullet.transform.position = laserEmitter.position;
bullet2.transform.position = laserEmitter2.position;
/*Vector3 rotation = bullet.transform.rotation.eulerAngles;
Vector3 rotation2 = bullet2.transform.rotation.eulerAngles;
bullet.transform.rotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, rotation.z);
bullet2.transform.rotation = Quaternion.Euler(rotation2.x, transform.eulerAngles.y, rotation2.z);*/
bullet.GetComponent<Rigidbody>().AddForce(laserEmitter.forward * laserSpeed, ForceMode.Impulse);
bullet2.GetComponent<Rigidbody>().AddForce(laserEmitter.forward * laserSpeed, ForceMode.Impulse);
StartCoroutine(DestroyBulletAfterTime(bullet, bulletDeathTime, bullet2));
}
Don't mind the commented lines, I was just messing around trying to see if I can get it to work. The shooting behaves the same with or without those commented lines.
Of course, a projectile based system will behave and look like a projectile system.
If you want laser behavior use a LineRenderer. Raycast where your laser line should end (either laser max distance or the point of hitting an object in range).
If you don't like the "static" looks of it, change the LineRenderer Material to something that changes over time (search for shaders/ LineRenderer effects).

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