Moving objects with angle and units in unity - c#

Hi I am learning unity/c# and I am stuck at a point please help...
I want a object like cube or sphere in 2 D environment to move to a particular spot without the use of x-axis and y-axis...
I want o specify the angle and units it should move. Like Move 4 units in 45 degree angle.
Thankyou alot

First you need to specify how many is 1 unit in your environment and then you can use trigonometrics to calculate your point.
and make a function like
public Vector2 GetCoordinates(float units, float angle){
//Your awesome trigonometics code here!
}
Note: A is your current position,B is the final position, and c is your units' and the angle cb is your angle.
b is your movement on x coordinate, and a is your movement in the y coordinate
Happy coding!
(if you need further explanation i can do it but try to figure out first :D)

Related

Spawning objects parallel to the terrain

Story
so I'm working on making a procedural terrain generator script to spawn rocks on the ground all the big rocks don't care what rotation they are but I have some small rocks that have to be parallel to the terrain or they look weird, part of them floating off the ground other part stuck in the ground the quaternions I don't seem to understand
Problem
i am getting the terrainData.GetInterpolatedNormal and putting it into a vector3 called normals then when i am spawning my rock i rotate it towards ground
Instantiate(SmallRock, new Vector3(point.x, Heights, point.y) transform.rotation = new Quaternion(normals.x,normals.y, normals.z, 90));
my problem lies in the Quaternion(normals.x,normals.y, normals.z, 90)
I don't know what to put where like should I only put the normals.x/z there or should I put the normals.y in there too and I don't even know what 90 at the end does, and yes I know that the interpolatednormals returns a 0 to 1 so I tried multiplying it to make it rotate more than 1 but it seems to just not rotate the right way if you can't tell by now I really have no clue how quaternions work but everywhere i search I can't find anything that helps and I didn't really feel like learning about how quaternions work so thanks for saving me time
Quaternions use compound(Imaginary) numbers to represent a sequence of rotations in 3d space.
When you instantiate a new Quaternion using it's constructor you are providing it with what you think the Quaternion's real and imaginary numbers should be.
Despite the seemingly familiar x, y, and z names you should not manually modify or provided them, they are not euler angles, cartesian coordinates, or traditional vector components.
What you're currently passing it is portions of a direction instead of the real and imaginary parts of a Quaternion.
A normal is an "outwards direction" from a given position. So to get a rotation we need some other direction to compare it to in order to get a rotation.
Compare your direction with the up direction and you'll get a rotation that you can use.
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, normalDirection);

Projectile Trajectory : Reaching specific coordinates

I am trying to implement a function in my game which will auto-lock a target and throw a projectile so that it lands perfectly on it.
I did the maths to calculate a parabola from Player 1 -> Target wherever their positions are but realised I wanted to use Unity's physics system rather than having the ball follow a path.
The throw velocity is constant, Player 1 and Target are moving objects but their positions will be registered once only to calculate the initial angle of the throw.
I believe this is the formula I need to use:
But how can I apply it for my Player and Target both having 3D coordinates?
Here is the pseudo-code of what I tried to write in Unity to make more easily readable.
float velocity = 100f;
float g = Physics.gravity;
Transform x = Target.position.x - Player.position.x;
Transform y = Target.position.z - Player.position.z;
double theta;
theta = **big formula using the values above**
And after that I do not know how to use this value to add force to the projectile.
I wanted to use AddForce(x,y,z, ForceMode.Impulse); but I clearly cannot use an initial angle here, only an x and y value.
Using RigidBody.velocity = Vector3(vx, vy, vz); gives me the same problem.
I believe I am missing something trivial but I really am stuck on this. Would anyone be able to help?

How to check if my NPC sees the back of a target?

I think the title says it all. Can it be done using only vector math?
var toTarget = (enemy.transform.position - npc.transform.position).normalized;
var seesBack = Vector3.Dot(toTarget, npc.transform.forward) < 0;
It seems I should somehow mix target's forward vector into the equation, but I'm really lame when it comes to vector math (well, math in general ;) ). Anyone?
EDIT:
I've also tried doing this, but the angle is too low. For example if the NPC is on the right of it's target, the calculated angle is ~60 degrees:
var angle = Mathf.Abs(Vector3.Angle(enemy.transform.forward * -1, npc.transform.forward));
var seesBack = angle <= 70;
Close! Consider that for the npc to see the back of the enemy, the enemy has to be looking roughly in the same direction as the vector from the npc to the enemy, aka toTarget:
var toTarget = (enemy.transform.position - npc.transform.position).normalized;
var seesBack = Vector3.Dot(toTarget, enemy.transform.forward) > 0;
Note that the Dot is against the enemy transform, and greater than 0.
Both of these tell you the same thing:
Are the two facing in the same direction?
Neither one tell you anything about which one is in front.
The dot-product one is going to be better, but you need to also check who's behind whom.
The one off-the-top-of-my-head way that I can think of is to compare the distance between the two entities, add a small amount along the NPC's forward vector (like, 0.5 units, something half the size of its collision volume's thickness) and get the distance from that point to the center of the other entity.
If the offset distance is smaller, the NPC is behind (as by moving forward, it would get closer). You'll probably also want a distance check involved somewhere as well, so that "behind" doesn't include "three rooms over." But I assume you've done that already.
Can it be done using only vector math?
This will check if the enemy is both facing away and positioned in-front of the NPC.
var r = Vector3.Dot(transform.forward, enemy.transform.forward) > 0f &&
Vector3.Dot(transform.forward, enemy.transform.position - transform.position) > 0f;

Bouncing a ball off a wall with arbitrary angle?

I'm trying to let the user draw a paddle that they can then use to hit a ball. However, I cannot seem to get the ball to bounce correctly because the x and y components of the ball's velocity are not lined up with the wall. How can I get around this?
I tried the advice given by Gareth Rees here, but apparently I don't know enough about vectors to be able to follow it. For example, I don't know what exactly you store in a vector - I know it's a value with direction, but do you store the 2 points it's between, the slope, the angle?
What I really need is given the angle of the wall and the x and y velocities as the ball hits, to find the new x and y velocities afterwards.
Gareth Rees got the formula correct, but I find the pictures and explanation here a little more clear. That is, the basic formula is:
Vnew = -2*(V dot N)*N + V
where
V = Incoming Velocity Vector
N = The Normal Vector of the wall
Since you're not familiar with vector notation, here's what you need to know for this formula: Vectors are basically just x,y pairs, so V = (v.x, v.y) and N = (n.x, n.y). Planes are best described by the normal to the plane, that is a vector of unit length that is perpendicular to the plane. Then a few formula, b*V = (b*v.x, b*v.y); V dot N = v.x*n.x+v.y*n.y, that is, it's a scalar; and A + B = (a.x+b.x, a.y+b.y). Finally, to find a unit vector based on an arbitrary vector, it's N = M/sqrt(M dot M).
If the surface is curved, use the normal at the point of contact.

Implementing a radar simulation

I'm doing a project on radar simulation and i have to detect how fast a plane is flying using c#.
Is there a formula which I can use to calculate the distance and the speed?
See the picture, R is the radar, P1 is the plane position at time 0, P2 is the plane position after time t. Since we know the speed of the radar wave in the air, we can calculate RP1 and RP2 easily. Also the angle P1RP2 is known, we can get length of P1P2 by trigonometric function. so the speed of the plane is P1P2/t.
There are different ways to determine the speed via radar. the one already mentioned, but also the change of frequency.
you might wanne check this out:
http://en.wikipedia.org/wiki/Doppler_radar
the distance between plane and radar is a result of the time used between transmitting and receiving.
d = c*t/2
If you have two know two points where the plane has been, and the time difference between these references, then it is very possible.
Speed it easy, calculate the distance usin g pythagorus:
float dist = sqrt( sqr(x2-x1) + sqr(y2-y1) );
Direction is trickier, and requires some trig. Try searching the internet for the formula for direction between two points.

Categories

Resources