I have two sprites and I am rotating them on mouse drag. When I rotate one, another has to rotate also but a bit slower. The issue is when I move first sprite too fast. I guess Unity skips some points where I ask for current rotation of object. Here is the code:
if(objekatKliknut=="Minute"){
mouseClickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = mouseClickPos - transform.position;
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
if (angle < 0.0f) angle += 360.0f;
angle = Mathf.Round(angle/6.0f)*6.0f;
if(angle==360) angle=0;
hand1.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
a1=angle;
if(a1!=a2){
float x = Mathf.Abs(a1-a2);
if(a1>a2){
moveRight = false;
if(x==6){
handRot+=addAngle*x/6f;
globeRot-=addGlobeAngle;
}
a2=a1;
}
else if(a1<a2){
moveRight = true;
if(x==6){
handRot-=addAngle*(x/6f);
globeRot+=addGlobeAngle*(x/6f);
}
a2=a1;
}
}
hand.transform.rotation = Quaternion.AngleAxis(handRot, Vector3.forward);
oblaci.transform.rotation = Quaternion.AngleAxis(globeRot, Vector3.forward);
}
hand1 is first object that I am rotating, and hand is the second one that needs to be rotated relative to the first one.
Can someone please help me?
Related
This problem is because when I want instantiating the projectile it doesnt come out the same way as the direction
Assuming your Angle is the same angle you want the projectile to have:
transform.rotation = Quaternion.AngleAxis(Angle, Vector3.forward);
If it's facing 90 degrees away from the correct direction, either rotate the sprite to face upwards, or try adding/substracting 90 to/form Angle like:transform.rotation = Quaternion.AngleAxis(Angle - 90, Vector3.forward);
Rewrite your CastSpell function to the following and tell me if it works:
void CastSpell(int slot) {
var spellObject = Instantiate(spellPrefab, transform.position, Quaternion.identity);
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var dir = mousePos - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 90;
spellObject.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
spellObject.GetComponent<SpellController>().srcPlayerID = playerID;
StartCoroutine(SpellCooldown(slot, gameConstants.spellCooldown));
}
I have a mini project where i have a car that drives on a cylinder and is rotating around the cylinder. When i press left mouse click the car rotates to the left and goes to the left and when i press right mouse click it rotates right and it goes right. What i want is when i release the mouse click buttons i want the car to rotate back to face the forward direction of the cylinder and continue going forward along the cylinder i have a script that is working but it is not rotating the car back to forward direction.
//THIS SCRIPT IS ATTACHED TO THE CAR
void Update(){
//CHECKS IF MOUSE BUTTON ARE CLICKED
if (Input.GetMouseButton (0)) {
rotation = -1;
} else if (Input.GetMouseButton (1)) {
rotation = 1;
} else {
rotation = 0;
}
}
void FixedUpdate(){
//moves the car forward
//rgb is the rigidbody of the car
rgb.MovePosition(rgb.position - transform.right * moveSpeed * Time.fixedDeltaTime);
// THE LINES BELOW ROTATE THE CAR TO THE LEFT OR RIGHT
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Quaternion deltaRot = Quaternion.Euler(yRot);
Quaternion targetRot = rgb.rotation * deltaRot;
rgb.MoveRotation(Quaternion.Slerp(rgb.rotation, targetRot, 50f * Time.fixedDeltaTime));
}
This is because you are setting the variable "rotation" to 0.
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Which results to 0 and your object doesn't rotate.
I would recommend you to do something like this in your update method:
if (Input.GetMouseButton (0))
{
rotation = -1;
}
else if (Input.GetMouseButton (1))
{
rotation = 1;
}
else
{
//Now when the rotation is positive,
//the object starts to rotate towards negative, but stops at 0
//Same is true when the rotation is negative
if (rgb.rotation.y > 0)
{
rotation = -1;
}
else if (rgb.rotation.y < 0)
{
rotation = 1;
}
else
{
//This needs to be added here to stop the object from rotating
//when the y rotation is already 0
rotation = 0;
}
}
Now it rotates towards 0 when you don't press anything. Also don't use Time.deltaTime in FixedUpdate.
Edit:
So I noticed that with this method it doesn't quite never return to 0. It gets very close but not close enough. By adding this to FixedUpdate the y rotation gets rounded to 0.
if (Mathf.Abs(targetRot.y) < 0.00001f)
{
targetRot = Quaternion.Euler(new Vector3(targetRot.x, 0, targetRot.z));
}
Am developing a follow target camera, it's working fine but when the vehicle(Target) is on slope, the camera is not rotating to show the full vehicle.
void LateUpdate()
{
if (car1.controlled && Camera.main != null)
{
float speedFactor = Mathf.Clamp01(target.root.GetComponent<Rigidbody>().velocity.magnitude / 20.0f);
if (speedFactor < 0.01f)
speedFactor = 0.01f;
Camera.main.fieldOfView = Mathf.Lerp(40, 65, speedFactor);
float currentDistance = Mathf.Lerp(13.5f, 8.5f, speedFactor);
currentVelocity = currentVelocity.normalized;
Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - ((currentVelocity * currentDistance));
newPosition.y = newTargetPosition.y;
Vector3 targetDirection = newPosition - newTargetPosition;
if (Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
Camera.main.transform.position = newPosition;
Camera.main.transform.LookAt(newTargetPosition);
}
LookAt is going to set the camera rotation to be looking straight at the newTargetPosition. It doesn't actually moves the camera up, it simply rotates it to look at the vehicule. You should modify your code so the camera moves up when the vehicule goes down a slope and then make it look at the vehicule.
Maybe its worth noting that I am using the Fingers asset for touch/mouse.
Okay so I have a turret that has a first person camera, on PC it uses the axis Mouse X/Y as I move the mouse around the camera follows. But I need for Mobile to work with the axis Mouse X/Y to drag the camera using touch.
What I need help with is:
As my fingers drags across the screen to move the camera's rotation not position. The position is modified by the vehicle.
This is the code I use for PC first person, moving the mouse:
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0f);
turretHeadToMove.rotation = rotation;
How would I use touch dragging to move the rotation? instead of mouse?
Any help with this? Thank you
Here is the current code for touch
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.GetTouch(0);
x += touch.deltaPosition.x * xSpeed * 0.02f;
y -= touch.deltaPosition.y * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0f);
turretHeadToMove.rotation = rotation;
}
}
For touch you could use
Input.GetTouch(0).deltaPosition
Try fiddle with something using this:
float strength = 2;
if (Input.touchCount == 1) {
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDirection = Input.GetTouch(0).deltaPosition;
// Update ur transform.Rotate the way u desire
// Different depending on using 1st person, 3rd person etc.
}
}
I have an object and 2 GUI texture buttons. I want to rotate the object to the left when I press the left button and to the right while pressing the other one.
Any ideas ?
I have a script that works when I drag my object. I will post the important part:
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate ()
{
if (isMouseOverGuiTexture()) return;
if (target && Input.GetMouseButton(0))
{
//0.1 represents the sensitivity of the mouse
x += Input.GetAxis("Mouse X") * xSpeed *0.1; //x rotation
//y -= Input.GetAxis("Mouse Y") * ySpeed *0.1; //y rotation
//y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.900528, 8.829305, -distance+0.49548)+ target.position;
transform.rotation = rotation;
transform.position = position;
}
}
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved it by using the following :
var imageLeft: Texture2D; // drag the left button image here
var imageRight: Texture2D; // right button image
var speed: float = 60; // rotate speed in degrees per second
private var rotLeft = false; // object rotates left if true
private var rotRight = false; // object rotates right if true;
function OnGUI()
{
rotLeft = GUI.RepeatButton(Rect(10,10,200,200), imageLeft);
rotRight = GUI.RepeatButton(Rect(230,10,200,200), imageRight);
}
function Update()
{
if (rotLeft) transform.Rotate(0, -speed*Time.deltaTime, 0);
if (rotRight) transform.Rotate(0, speed*Time.deltaTime, 0);
}
I don't know which value Time.deltaTime assumes inside OnGUI - it should be the time since last OnGUI, but I'm not sure. To avoid problems, I placed the real rotation in Update and used two control booleans (rotLeft and rotRight) to communicate with OnGUI.