using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetBehaviour : MonoBehaviour
{
// Add this script to Cube(2)
[Header("Add your turret")]
public GameObject Turret;//to get the position in worldspace to which this gameObject will rotate around.
[Header("The axis by which it will rotate around")]
public Vector3 axis;//by which axis it will rotate. x,y or z.
[Header("Angle covered per update")]
public float angle; //or the speed of rotation.
public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness;
// Update is called once per frame
void Update()
{
//Gets the position of your 'Turret' and rotates this gameObject around it by the 'axis' provided at speed 'angle' in degrees per update
transform.RotateAround(Turret.transform.position, axis, angle);
time += Time.deltaTime;
//Sets value of 'height' randomly within 'upperLimit' & 'lowerLimit' after delay
if (time > delay)
{
prevHeight = height;
height = Random.Range(lowerLimit, upperLimit);
time = 0;
}
//Mathf.Lerp changes height from 'prevHeight' to 'height' gradually (smooth transition)
transform.position = new Vector3(transform.position.x, Mathf.Lerp(prevHeight, height, time), transform.position.z);
}
}
In general it's working the problem is for example if I set the axis x,y,z to 1,1,1 on the variable: axis
And Angle set to 1 Upper limit to 50 Lower limit to 2 and delay to 2.
Then the object is making a circle around the other object but sometimes when the object is getting higher the most he get higher the object making a bigger circle and then when the object is getting lower the circle is smaller.
How can I make it to keep the circle radius static ?
The main goal is to move the object in circles around another object in random highs limits for example 2 and 50 but I want to keep the same radius all the time. Now the radius is changing by depending on the height.
As you are constantly moving the object upwards, if you want the radius of rotation to remain constant then the axis of rotation must veertical ie - Vector3.up or new Vector3(0, 1, 0)
Related
trying to rotate object both in sinusoidal and circular form along 3 axis
using UnityEngine;
public class time_behaviour : MonoBehaviour
{
float time;
public float rate;
public float x_axis_amplitude;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
time =Time.time;
float x_rotation = x_axis_amplitude*Mathf.Asin(Mathf.Pow(Mathf.Sin(time*rate),2))*Mathf.Rad2Deg;
float y_rotation = 2*(Mathf.Acos(Mathf.Cos(time*rate))*Mathf.Rad2Deg);
transform.localRotation = Quaternion.Euler(x_rotation,y_rotation,0);
Debug.Log(transform.rotation.eulerAngles);
}
}
here is the code
as you can see i am trying to revolve object in sin form along x axis and circularly along y axis
i got desired outcome for x axis but for y axis the rotation keep flipping after 360 degree ,
Is there anyway that after 360 degree it dont flip its motion and rotate forever
I have tried doing transform.Rotate() but it dont rotate uniformly,it just keeps object stationary at that angle
I'm working on a game and in this game the trees are 2d , because of this i want them to rotate around the player (i used this script)
{
public Transform target;
void FixedUpdate()
{
transform.LookAt(target);
}
}
but when i get to close to the quad it goes above me so
How can i limit the quad's rotation on X?
Your problem is simply that you can change A to rotate around B.
Method 1: B does not move, and A hangs the script to implement transform's RotateAround(vector3 point, vector3 axis, float angle) function
For example A.RotateAround(B.position, Vector3.up, 30*Time.deltaTime); //A rotates around the y-axis of B.
Method 2: A does not move, A acts as a sub-object of B, and B hangs the script to achieve rotation, and then rotates with A, similar to simulating rotation around the central celestial body.
For example: B.Rotate (Vector3.up, 30*Time.deltaTime, Space.Self); //B rotates around its own up direction, thus rotating with A.
Common methods of rotation:
(1) Rotate around the coordinate axis
public void Rotate (Vector3 eulerAngles, Space relativeTo = Space.Self);
or
public void Rotate (float xAngle, float yAngle, float zAngleSpace, relativeTo = Space.Self);
Where relativeTo = Space.Self means to rotate around its own coordinate system, and ralativeTo = Space.World means to rotate around the world coordinate system.
(2) Rotate a certain vector
public void Rotate(Vector3 axis, float angle, Space relativeTo);
where axis is the direction of the rotation axis, angle is the rotation angle, relativeTo is the reference coordinate system, and the default is Space.self. The function of this method is to rotate the GameObject object by angle degrees around the axis in the relativeTo coordinate system.
(3) Rotate around the pivot point
public void RotateAround(Vector3 point, Vector3 axis, float angel);
The function is to rotate the GameObject by an angle degree around the axis of the point.
I have a "Lift". While being in the game, you walk into the particle system and get moved up in the air (on y).
So the particle system is a child of the cube / the lift. So when scaling the cube, I don't want to change the settings of the particle system. It should scale itself on its own.
When the cube got the y position on 5 and a height / scaling on y of 10, the particle system should place itself down at the bottom.
As you can see, I want it being full automatic.
So, when heading into the code I got this
[SerializeField]
ParticleSystem liftParticles;
private void Start()
{
Vector3 objectScale = transform.localScale; // cube scaling
Vector3 particlePos = liftParticles.transform.position; // temp position
particlePos.y = (particlePos.y - objectScale.y) / 2; // move down on y
liftParticles.transform.position = particlePos; // set particle position
float transformScalingX = objectScale.x; // x scaling of the cube
float transformScalingZ = objectScale.z; // z scaling of the cube
var shape = liftParticles.shape; // set the cone radius now
shape.radius = transformScalingX > transformScalingZ ? transformScalingX : transformScalingZ;
liftParticles.shape = shape;
}
I want to go with the following example as mentioned above..
The cube got a scaling of (3,10,3) and its position is (0,5,0)
my current calculation particlePos.y returns a value of -0,75 but it has to be -0,5.
So do I have an error in my code? (yes obviously I do ...)
The second problem is, how do I change the radius of the particlesystem? When trying to reference the radius of the cone, it says I can't change it, it is readonly.
Is it? I hope I can change this somehow ...
Edit:
Obviously, the particlesystem just has to be always on -0,5f on y when having a scaling of (1,1,1). No need for a calculation anymore.
But I still need to change the radius of the shape and set the lifetime of the particles relative to the height of the lift. Means
private void Start()
{
Vector3 liftScale = transform.localScale; // Liftscaling
var shape = liftParticles.shape; // temp shape
shape.radius = liftScale.x > liftScale.z ? liftScale.x : liftScale.z; // set radius
liftParticles.shape = shape; // assign the temp shape to the real shape
liftParticles.main.startLifetime = ; // set the liftetime of the particles relative to its parent height on y
}
As I understand you made the particle system child of the lift (a cube) so it can move together. In case you just want that both of them move together, but they scale independently, you should consider to use an Empty GameObject as a parent.
You can placed this Empty GameObject in the middle of the Cube (your lift) and then make the lift and the particle filter children of that Empty GameObject. Then move the Empty GameObject instead of the lift, and the children will move as well.
About modifying the radio, try this script
GameObject myParticleGenerator;
ParticleSystem.ShapeModule pShape;
pShape = yParticleGenerator.GetComponent<ParticleSystem>().shape;
pShape.radius = 4.0f;
I am making a 2D plant simulation using 2D Sprites in Unity. The plants are represented as circular sprites. What I want to do is to increase the radius of the sprite and have the CircleCollider2D follow along. Since they have different scalings I am using a procuental increase of the radius instead of an absolute increase. But the problem is that the collider radius increases faster than the sprite, even though the relation between the collider radius and the sprite radius stays the same.
Here is the code which increases the size:
void Grow(GameObject plant)
{
float currentRadius = plant.GetComponent<CircleCollider2D>().radius; //Radius of the collider
float scaledRadius = Mathf.Max(plant.transform.localScale.x, transform.localScale.y); //Radius of the sprite
float radiusIncrease = growthSpeed * Time.deltaTime;
float procIncrease = (currentRadius + radiusIncrease) / currentRadius; //The procuental increase of the radius, need for sprite scaling
plant.GetComponent<CircleCollider2D>().radius *= procIncrease;
Vector3 oldLocalScale = plant.transform.localScale;
Vector3 newLocalScale = new Vector3(oldLocalScale.x * procIncrease, oldLocalScale.y * procIncrease, oldLocalScale.z);
plant.transform.localScale = newLocalScale;
}
I found out that the radius of the CircleCollider2D increases with the sprite, which means that I don't have to change the radius of the collider myself.
I want to make an object (let's say a cube), rotate at a steady rate on the same axis that's it's moving on. So if it changes direction from X to Z then the rotation would lerp from X axis into the Z axis and then continue rotating on Z axis.
How would I achieve this? Here's what I have at the moment, the cube just rotates on the z axis back and forth within a certain degrees.
public float Angle;
public float Period;
void Update()
{
Animate();
}
void Animate()
{
_time = _time + Time.deltaTime;
float phase = Mathf.Sin(_time / Period);
transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, phase * Angle));
}
Just use
RotateAround
Note generally NEVER use Quaternion for any reason.
there are 1000s of questions on using RotateAround so just google. In your case it sounds like you'll be changing (lerping, whatever) the axis of rotation itself.