Cannot freeze rotation on an object that has translate function on it - unity3d

I'm building a game, same as the games where rockets follows a plane to destroy it. I have a problem at the rocket. It is 3D and when the game start the rocket is rotating 90 degrees and I don't understand why, and how could be fixed.
I tried to use constrains from rigidbody, in inspector, but those didn't worked.
this is the script that controls the rocket.
public class Move_missle_lookAt : MonoBehaviour
{
public Transform mTarget;
public float mSpeed ;
const float EPSILON = 0.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.LookAt(mTarget.position);
if ((transform.position - mTarget.position).magnitude > EPSILON)
transform.Translate(0.0f, 0.0f, mSpeed * Time.deltaTime);
}
}

It's possible that your model is oriented incorrectly to start with. So, althought it appears to be rotated, it's actually in its native orientation.
After you call LookAt(), rotate the transform by 90° to compensate.

Related

Rotate an object around a point in unity

I'm new to Unity. So just for an experiment, I want to create a canon by attaching a rectangle to a circle and when the up arrow key is pressed, the canon change firing angle. So I have a rectangle object that is a sub object of a circle. And then I created a script in C# for the circle object.
Here is the codes that I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float rotation = 0f;
private float timeValue = 0.0f;
public GameObject wheele;
private float xMin = -1.0f, xMax = 1.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (rotation >= -90)
transform.Rotate(new Vector3(0.0f, 0.0f, rotation));
rotation -= 2;
Mathf.Clamp(rotation, -90.0f, 0);
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
if (rotation >= -90)
transform.RotateAround(wheele.transform.position, Vector3.up,20);
rotation += 2;
Mathf.Clamp(rotation, -90.0f, 0);
}
}
}
I tried both transform. Rotate method, but it rotate around the center of the rectangle. But we need the rectangle to rotate with the axis, the center of the circle.
You're asking how to get the pivot point changed, right? Make an empty game object and drag the cannon under it to make it a child, then drag the cannon to a point which you think is fine and rotate the empty game object instead of the cannon, which pretty much just changes the pivot point
There are two main ways.
Update your model to set the pivot point exactly where you want it to rotate. This is how you would do this using blender. This would require you to actually create a model for the canon though (even if it's as simple as the one you've used Unity primitives for)
Create a parent GameObject, and rotate it instead. You can offset the cannon inside this GameObject, to compensate for the pivot point set on the model.

After rotate gameobje with joystick , rotate resetting. joystick rotate correction

After rotating the character with jotstick. rotate resetting. direction of the character With the joystick, I want the character to look in that direction when I turn my hand in the direction I want and then pull my hand out of the joystick. Don't let him look in the same direction joysticki when I left. how can I do it. thanks.
public class MyJoystick : MonoBehaviour
{
public Joystick joystick;
public Joystick joystickRot;
public float moveSpeed;
Quaternion targetRotation;
Rigidbody rigidbody;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// var rigidbody = GetComponent<Rigidbody>();
rigidbody.velocity = new Vector3(joystick.Horizontal * moveSpeed, rigidbody.velocity.y, joystick.Vertical * moveSpeed);
// this is problem
// don't reset the rotate when joysticki is released.
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Atan2(joystickRot.Horizontal
, joystickRot.Vertical) * Mathf.Rad2Deg, transform.eulerAngles.z);
}
}
'joystickRot' will return zeros when not being pushed in any other direction, that is why your rotation is being reset, so you must check first that the joystick is actually being used before applying the values to your GameObjects transform rotation.
I'm not overly familiar with the joystick system you are using, but there will most certainly be a way to test if the joystick is being used or not, and only apply the force and rotations when it is.
By the way, you have two Joystick objects referenced, but I believe you only need one.

Smooth Camera Follow with Voxel Based Sphere

I am very new to Unity and just got done yesterday following the Roller Ball example on the learn page here at Unity3d.
To practice what I have learned I wanted to try and recreate something similar using my own art and making the game different. I have been playing around with Voxel Art and I am using MagicaVoxel to create my assests. I created the walls, the ground etc.. and all is well.
Then came the player object, the sphere. I created one as close to a sphere as possible with magicaVoxel and it rolls fine. However, when using a script to have the camera follow the object it runs into issues.
If I don't constrain the Y axis then I will get bouncing and as far as the x and z axis I get kind of a Flat Tire effect. Basically the camera doesn't follow smoothly it bounces around, stop go etc...
I have tried making the collider larger then the sphere and even using the position of the collider vs the object itself. I have also tried putting the code in Update / FixedUpdate / LateUpdate. What is the proper way to fix or address something like this? Here is my scripts below:
Camera Controller:
public class CamController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
// Get the distance between the player ball and camera.
offset = this.transform.position - player.transform.position;
}
void LateUpdate ()
{
this.transform.position = player.transform.position + offset;
}
}
Player Controller:
public class PlayerController : MonoBehaviour {
public float _speed;
void FixedUpdate()
{
// Get input from keyboard.
float _hoz = Input.GetAxis("Horizontal");
float _ver = Input.GetAxis("Vertical");
// Create a vector3 based on input from keyboard.
Vector3 _move = new Vector3(_hoz, 0.0f, _ver);
// Apply force to the voxel ball
this.GetComponent<Rigidbody>().AddForce(_move * _speed);
}
}
Thanks for any help in advance.
You can use the SmoothFollow Script of Unity it self for getting smooth follow of camera.
Here are the steps how you can get the script:
1) Assets->Import Package->Scripts.
2) At the dialog that appears select all the scripts, or just the smooth follow one and hit Import button.
3) Now this script is in your project, and you can attach it to the camera.
Hope this will help you...
Best,
Hardik.

Camera Move and Rotate Follow Player very jerky

I make a character move on the surface of a circle. I let the camera move and rotate follow character. But the camera move and rotate very jerky. If I increase the value of the third parameter, the shock increases. and to reduce the value of the third parameter, the camera does not rotate to keep up the character. Help me fix it
My Code Camera Follow Player
public class CameraFollow : MonoBehaviour
{
public Transform player;
GameController gc;
public float speed = 2;
Vector3 pos = new Vector3 (0, 0, -10);
// Use this for initialization
void Start ()
{
gc = FindObjectOfType (typeof(GameController)) as GameController;
}
void FixedUpdate ()
{
if (gc.gameState == GameController.GameState.playing || gc.gameState == GameController.GameState.changeWave) {
transform.position = player.position + pos;
transform.rotation = Quaternion.Slerp (transform.rotation,
player.transform.rotation,
speed * Time.deltaTime);
}
}
}
Setting the position of a transform inside of FixedUpdate is a red flag for sure, especially when you're reporting that it's "jerky". Fixed update happens at an irregular interval compared to the frames displayed. This is because Physics needs to update using a fixed time step. The reason why this is the case is out of scope for this question.
Long story short, try changing FixedUpdate to Update and that should fix things looking "jerky".
Let me know if this doesn't work and I'll look for other possible causes.
If you are using a Rigidbody2D to move the character, make sure to set its Interpolate property to 'Interpolate'. This should fix it.

Unity 3D realistic accelerometer control

How do we achieve a control similar to this game?
https://play.google.com/store/apps/details?id=com.fridgecat.android.atiltlite&hl=en
You can do this with builtin physics:
create a level from some simple scaled cubes (don't forget the ground).
add the ball - a sphere, then and add a RigidBody to it. Set a constraint on the rigidbody - check freeze position y (or it will be able to jump out of the level if you put the device upside down).
add this script anywhere on the scene (for example to the camera):
using UnityEngine;
public class GravityFromAccelerometer : MonoBehaviour {
// gravity constant
public float g=9.8f;
void Update() {
// normalize axis
Physics.gravity=new Vector3(
Input.acceleration.x,
Input.acceleration.z,
Input.acceleration.y
)*g;
}
}
or if you want the physics to just affect this one object, add this script to the object and turn off it's rigidbody's affected by gravity:
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class ForceFromAccelerometer : MonoBehaviour {
// gravity constant
public float g=9.8f;
void FixedUpdate() {
// normalize axis
var gravity = new Vector3 (
Input.acceleration.x,
Input.acceleration.z,
Input.acceleration.y
) * g;
GetComponent<Rigidbody>().AddForce (gravity, ForceMode.Acceleration);
}
}
And now you have working ball physics. To make the ball behave as you'd like, try to play with the rigidbody properties. For example, change the drag to something like 0.1.