unity3d rotate back forth with limited angles - unity3d

I'm want to rotate an object on an axis for specific angles over period of time.
And repeat that on the reverse direction once it reached the limit (of let's say 5 degree).
I could use Quaternion.Euler() to do the rotation towards 5 degree, but how do I check whether it has reached 5 degree and start reversing the direction towards -5 degree?
so in Update() I do:
int dir = 1; // somewhere global
Quaternion r = Quaternion.Euler(0, Timer.deltaTime * dir, 0);
transform.rotation *= r;
// I want to: if the "angle is >= 5f", i want to do dir *= -1 to reverse it
if (/* angle delta is >= 5f or <= -5f */)
{
dir *= -1;
}
Thanks

If you just want to rotate back and forth, you can use a sine wave to move back and forth smoothly.
public class rotator : MonoBehaviour {
public float _Angle;
public float _Period;
private float _Time;
// Update is called once per frame
void Update () {
_Time = _Time + Time.deltaTime;
float phase = Mathf.Sin(_Time / _Period);
transform.localRotation = Quaternion.Euler( new Vector3(0, phase * _Angle, 0));
}
}

Related

Unity quaternion lookrotation behaving oddly

I am using unity to create an airline game. I made the takeoff code as the following:
`
Rigidbody m_Rigidbody;
float thrust_x = 2;
float thrust_y = 0;
float timeRemaining = 1.5f;
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
void TakeOff()
{
m_Rigidbody.AddForce(thrust_x, thrust_y, 0, ForceMode.Impulse);
transform.rotation = Quaternion.LookRotation(new Vector3(thrust_x, thrust_y, 0));
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
}
else if (timeRemaining < 0 && thrust_y < 1.5)
{
thrust_y += 0.1f;
}
}
`
The timeRemaining waits for 1.5 seconds and then add to the thrust_y. The lookrotation makes the plane point in the direction of the impulse vector. This works impeccably with the plane as a cube. When I replaced it for a model, the plane strangely adds 90deg to the y rotation for no apparent reason.
Before running:
While running:
How can I make the plane once again point to 0,0. Turning the plane 90 and -90 at the start does not work, which gives reason that the value is being set, not changed.
Any help will be greatly appreciated. Thanks!

2D car control with mouse position in unity 2D

i am new in unity. I want to car game with mouse control to Unity 2D. I was trying this code but not working. Car vibrates when i move mouse over car. I want it to work perfectly when the mouse hovers over the car. how can i do this? my code is as follows:
private void OnMouseOver()
{
// Distance from camera to object. We need this to get the proper calculation.
float camDis = cam.transform.position.y - my.position.y;
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
body.rotation = angle;
Vector3 temp = Input.mousePosition;
temp.z = 10f; // Set this to be the distance you want the object to be placed in front of the camera.
this.transform.position = Camera.main.ScreenToWorldPoint(temp);
}
I'm not too clear on the effect you want to achieve, but if you just want the object to move and turn gradually instead of instantly changing, that can be achieved using Vector3.MoveTowards and Quaternion.RotateTowards, e.g.:
private void OnMouseOver()
{
// Distance from camera to object. We need this to get the proper calculation.
float camDis = cam.transform.position.y - my.position.y;
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
//body.rotation = angle; //??
float turnSpeed = 200f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, angle), turnSpeed * Time.deltaTime);
Vector3 temp = Input.mousePosition;
temp.z = 10f; // Set this to be the distance you want the object to be placed in front of the camera.
float moveSpeed = 10f;
transform.position = Vector3.MoveTowards(transform.position, Camera.main.ScreenToWorldPoint(temp), moveSpeed * Time.deltaTime);
}
Edit in response to comment: If you want it to move only when the player begins the drag on the car, then yes, putting it in OnMouseDrag() would work. If you want it to move when the player drags from anywhere on the screen, you'd want to put the movement code in Update() and check whether the left mouse button is being held down using Input.GetMouseButton(0).
If you wanted it to keep moving towards the last mouse position (e.g. player can click on the screen and it will move there while the mouse button is not being held down), you'd need to keep the last mouse location in a class variable and move towards that in Update().
Incidentally if you want it to move a bit more like a car, you could always move it forwards while it turns towards the mouse, rather than moving it directly towards the mouse even if it's facing a different direction.
Here's an example but be aware that I've changed a few things that didn't seem necessary to me, like using my.position rather than transform.position. If you use it you may need to adapt it to suit the rest of your code.
public float maxTurnSpeed = 250f;
public float maxSpeed = 8f;
public float stopDistance = 0.5f;
public float slowDistance = 2.5f;
private void Update()
{
if( !Input.GetMouseButton(0) ) // If the mouse button is NOT being held down this frame
return; // Don't move. (Ideally you would decelerate the car rather than stopping it immediately though.)
// Remove the above two lines and move all of this to OnMouseDrag if you want to require the drag to begin on this object to move it.
// Also note: this code now assumes the object begins in the desired z position and doesn't change it, rather than forcing a z position.
// Distance from camera to object. We need this to get the proper calculation.
float camDis = transform.position.z - Camera.main.transform.position.z; // Changed this to use z instead of y as it appeared to be a mistake(?)
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camDis));
float distanceFromMouse = Vector3.Distance(transform.position, mouse);
// If we're further away from the mouse than stopDistance, move.
if( distanceFromMouse > stopDistance )
{
float speedMultiplier = 1.0f;
float rotationMultiplier = 1.0f;
// If we're closer to the mouse than slowdistance, slow down proportionately to the remaining distance from stopDistance
if( distanceFromMouse < slowDistance )
{
speedMultiplier = (distanceFromMouse - stopDistance) / (slowDistance - stopDistance);
}
// Reduce turning speed as we approach stopDistance, but not by as much as speed is reduced
if( speedMultiplier < 0.5f )
rotationMultiplier = speedMultiplier * 2f;
float AngleRad = Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
// Turn the car towards facing the mouse position
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, angle), maxTurnSpeed * rotationMultiplier * Time.deltaTime);
// Move the car towards its transform.right vector.
transform.position += transform.right * (maxSpeed * speedMultiplier * Time.deltaTime);
}
}

Unity - Projectile Motion, find the angle needed to hit coordinates x,y

Plenty of questions asked around the same topic, but nothing seems to be working for me.
The problem is simple, a player and an enemy are on the x,y plane. I want to launch my projectile at a calculated angle in such way that the projectile will hit the enemy at it's coordinates.
I've tried implementing both
Angle of Reach and Angle required to hit x,y
Both of these implementation end up doing the same for me; Shooting but not hitting the target in this manner
Any help or pointers would be much appreciated! Thank you
Here is the code:
public Rigidbody projectile;
public float projectileSpeed;
public float Firerate = 9f;
private float nextfire;
private GameObject enemy;
private float gravity = Physics.gravity.y;
private Vector3 directionalVector;
// Start is called before the first frame update
void Start()
{
enemy = GameObject.FindGameObjectWithTag("enemy");
}
void Update()
{
directionalVector = enemy.transform.position - transform.position;
}
void FixedUpdate()
{
nextfire = Time.time + (1 / Firerate);
float projectileSpeed2 = projectileSpeed * projectileSpeed;
float projectileSpeed4 = projectileSpeed2 * projectileSpeed2;
float x = enemy.transform.position.x;
float y = enemy.transform.position.y;
float x2 = x * x;
float theta = Mathf.Atan(projectileSpeed2-Mathf.Sqrt(projectileSpeed4-gravity*(gravity*x2+2*y*projectileSpeed2))/gravity*x);
print(theta);
Vector3 releaseVector = (Quaternion.AngleAxis(theta, Vector3.up) * directionalVector).normalized;
Debug.DrawRay(transform.position, releaseVector, Color.red,0.5f);
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = releaseVector * projectileSpeed;
}
}
Why not avoid the problem of finding the angle, and just move the bullet based on the direction on where it first saw the enemy.
(target.transform.position - transform.position).normalized;
It will return a Vector direction to the target.
When the projectile moves, just move it based on this direction.
No headache needed in calculating angles :)
Edit
I made a function before to 'convert' an angle to direction:
protected Vector2 DetermineBulletMoveDirection(float shootingAngle) {
// Determine the direction of the bullet travel on the x and y axis.
float bulletDirectionX = transform.position.x + Mathf.Sin((shootingAngle * Mathf.PI) / 180);
float bulletDirectionY = transform.position.y + Mathf.Cos((shootingAngle * Mathf.PI) / 180);
// Determines the direction this bullet should be moving.
Vector2 bulletDirection = new Vector2(bulletDirectionX, bulletDirectionY);
return (bulletDirection - (Vector2)transform.position).normalized;
}
It takes in an angle, and converts it into a direction based on where the shooter is currently at.
The angle should start from Vector.down, and rotates clockwise.
The next problem is to find out the angle between you and the enemy.
This is the simplest solution I could think of, here is a diagram first:
Notice that you can use TOACAHSOH on this?
So all you have to do, is to 'virtually' align the Y axis of the shooter to the origin.(Apply the movement to the shooter too!)
Do the same thing for the shooter, but on the x-axis this time.
And you would be able to achieve that state where you have a triangle with a 90-degree.
From there on, you can calculate the angle to rotate from Vector.down to the enemy.
Just make sure you move both of the objects back to it's initial position.
After fighting this for a while I found a solution.
In the end I ended up using the Angle of Reach. The second error was that Mathf.Atan returns radians and not degrees, while Quantion.AngleAxis takes in angles. The third and the final one was the fact that Unity uses left hand coordinate system as opposed to the usual right hand system which I was used to.
Here is the final piece of code:
public class TargetAndShoot : MonoBehaviour
{
public Rigidbody projectile;
public float projectileSpeed;
public float firerate;
private float nextfire;
private GameObject enemy;
private float gravity = Physics.gravity.y;
// Start is called before the first frame update
void Start()
{
enemy = GameObject.FindGameObjectWithTag("enemy");
}
void Update()
{
if (Time.time >= nextfire)
{
nextfire = Time.time + (1 / firerate);
float distance = enemy.transform.position.x - transform.position.x;
Vector3 directionalVector = enemy.transform.position - transform.position;
float v2 = projectileSpeed * projectileSpeed;
float v4 = v2 * v2;
float x = enemy.transform.position.x;
float x2 = x * x;
float y = enemy.transform.position.y;
float theta = 0.5f*Mathf.Asin((gravity * distance) / (projectileSpeed * projectileSpeed));
Vector3 releaseVector = (Quaternion.AngleAxis(theta * Mathf.Rad2Deg, -Vector3.forward) * directionalVector).normalized;
Debug.DrawRay(transform.position, releaseVector*5, Color.cyan, 0.5f);
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = releaseVector * projectileSpeed;
}
}
}

Ball Mechanics - Is this the best approach?

Good day,
I'd like to program a constantly moving ball (object3) being passed between two stationary objects (object1, object2), with the ability to set the max height Y of the pass trajectory dynamically.
What would you argue is the best way to program the ball physics for this concept?
I've looked at using addForce on a default sphere w/ a rigidbody. It seems like there should be an equation that expresses the trajectory of a pass of object3 from object1's x to object2's x... at a known, given speed, with a known, set mass, and a known gravity environment.
However, currently I have a Vector3.Lerp interpolating the ball between the two objects on each FixedUpdate() with t expressed as:
`(Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;`
It works and all, but with this approach, it seems there's no clear way to add height to the trajectory of the ball path. I've considered adding the height to the Y value in object2 until the ball is half way there, and then setting it back to the original Y position... but it just feels wrong! Thoughts?
Thanks!
Okey so if I understand you correctly currently you are doing
privte void FixedUpdate()
{
var factor = (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f;
object1.MovePosition(Vector3.Lerp(object2.position, object3.position, factor));
}
which moves the ball pingpong between object1 and object2 position but only planar.
Assuming for now the objects will only be moving within the XZ plane and never have different Y position in order to rather get a curve with height you could treat the separatly:
- Interpolate between both positions as before
- Separately calculate the Y position with sinus or any other mathematical curve function - for realistic physics probably rather a parabola actually
Could look somhow like
public class Example : MonoBehaviour
{
public Rigidbody object1;
public Transform object2;
public Transform object3;
// adjust in the Inspector
public float speed = 1;
public float Amplitude = 0;
// Just for debug
[Range(0, 1)] [SerializeField] private float linearFactor;
[SerializeField] private float yPosition;
private void FixedUpdate()
{
// This always returns a value between 0 and 1
// and linearly pingpongs forth and back
linearFactor = Mathf.PingPong(Time.time * speed, 1);
// * Mathf.PI => gives now a value 0 - PI
// so sinus returns correctly 0 - 1 (no need for +1 and /2 anymore)
// then simply multiply by the desired amplitude
var sinus = Mathf.Sin(linearFactor * Mathf.PI);
yPosition = sinus * Amplitude;
// As before interpolate between the positions
// later we will ignore/replace the Y component
var position = Vector3.Lerp(object2.position, object3.position, linearFactor);
object1.MovePosition(new Vector3(position.x, yPosition, position.z));
}
}
You could optionally also try and add some dumping in the Y direction in order to make the vertical movement more realistic (slow down when reaching the peak). I tried a bit using inverted SmoothStep like
// just for debug
[Range(0, 1)] [SerializeField] private float dampedSinusFactor;
[Range(0, 1)] [SerializeField] private float linearFactor;
[SerializeField] private float yPosition;
private void FixedUpdate()
{
// Use two different factros:
// - a linear one for movement in XZ
// - a smoothed one for movement in Y (in order to slow down when reaching the peak ;) )
linearFactor = Mathf.PingPong(Time.time * speed, 1);
dampedSinusFactor = InvertSmoothStep(linearFactor);
// * Mathf.PI => gives now a value 0 - PI
// so sinus returns correctly 0 - 1 ()
// then simply multiply by the desired amplitude
var sinus = Mathf.Sin(dampedSinusFactor * Mathf.PI);
yPosition = sinus * Amplitude;
// later we will ignore/replace the Y component
var position = Vector3.Lerp(object2.position, object3.position, linearFactor);
object1.position = new Vector3(position.x, yPosition, position.z);
}
// source: https://stackoverflow.com/a/34576808/7111561
private float InvertSmoothStep(float x)
{
return x + (x - (x * x * (3.0f - 2.0f * x)));
}
However for slow movements this looks a bit strange yet. But you can come up with any other maths curve that results in the expected behavior for x=[0,1] ;)

Camera rotation with mouse but dont stop

I have a question I made a game where you controll an spacecraft and use the mouse/keyboard to move. I use the mouse to rotate the ship up, down left and right. Works like a charm. Only problem is when I stop moving the mouse the ship also stops. So If I want to rotate the ship a few times I need to run a marathon with my mouse. This is because I use the Input.Axis(Mouse X) and thats zero when you stop moving the mouse.
So what I want is: Rotate the ship lets say left if I move my mouse a bit to the left and only stop when I return the mouse to the centerarea. I have this code ATM
var c = Camera.main.transform;
float mouseX = Input.GetAxis("Mouse X");
c.Rotate(0, mouseX * sensitivity, 0);
c.Rotate(-Input.GetAxis("Mouse Y") * sensitivity, 0, 0);
c.Rotate(0, 0, -Input.GetAxis("QandE") * 90 * Time.deltaTime);
How can I accomplish this
BugFinder's answer is correct. Here it is in code, for rotating around the Y axis. You can build on it to get your other axes working.
using UnityEngine;
public class Roll : MonoBehaviour
{
public float sensitivity = .001f;
Transform c;
void Start()
{
c = Camera.main.transform;
}
void Update()
{
Vector3 mouse = Input.mousePosition;
float dx = 0;
if (mouse.x > 1000)
dx = mouse.x - 1000;
if (mouse.x < 920)
dx = mouse.x - 920;
c.Rotate(0, dx * sensitivity, 0);
}
}