Trying to make a smooth rotation script - c#

I'm kind of new in learning Unity scripting and currently i'm writing a script where by pressing the Space key together with A or D key would do some sort of a "dash" movement by rotating faster.
In the gif below I show the basic A and D movement without dashing movement. What i've done is that i've put an empty with the script and an object parented to the rotating empty.
the example
What I wanted to do is that whenever the player wanted to go faster on a direction, it would "dash" on the left by pressing A+Space and on the right by pressing D+Space. I kind of managed to do this thing with this code (where rotSpeed is the rotation speed and dashAmount is the speed added to make the "dash"):
transform.Rotate(dashAmount + rotSpeed * Time.deltaTime, 0f, 0f);
Of course, this worked but the cube would just teleport instead of "dashing" smoothly to the left or right. Then i found out about Quaternion.Lerp but the code i wrote doesn't really work correctly (destinationRot is the final rotation and dashSpeed is set to 0.8f)
if ((Input.GetKey(KeyCode.Space)) && (Input.GetKey(KeyCode.D)))
{
Quaternion destinationRot = Quaternion.Euler(transform.rotation.x + dashAmount, 0f, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, destinationRot, Time.deltaTime * dashSpeed);
}
How can I fix this?

Related

How to make objects rotate the same using transform.rotate in unity

I have several objects that pass through a circular trigger collider, I have made it so when the objects enter the collider they start rotating using transform.Rotate() and when they exit the collider they stop rotating. My problem is that every objects has a different rotation by the time they exit the collider. Is there any way to make them rotate consistently?
void Update()
{
if(rotate==true)
{
transform.Rotate(rotation);
}
direction = (carrot.position - transform.position).normalized;
transform.position += new Vector3(speed * direction.x * Time.deltaTime, speed * direction.y * Time.deltaTime, 0);
}
Here is my code, I simply made it so when the object enters the collider, rotate becomes true and when it exits, rotate becomes false. rotation is a Vector 3 variable that does not change.
You could try rotating the object within FixedUpdate.
This should give you some determinism.

Make a Unity 3D body move in a circle from it's launch point?

I'm playing with some Unity code to expand my horizons.
I have the following code, which launches a bullet from the character that flies in a direction set by 'this.angle' which is the angle the gun is pointing to the target.
What I'm trying to accomplish is to make the bullet leave the gun and travel 360 degrees right around and shoot the player (actually the gun really) from behind.
I'm not fully understanding quarternions, but I don't think I need to, to solve this.
Could someone please give me a pointer?
base.transform.rotation = Quaternion.Euler(0f, Mathf.MoveTowardsAngle(base.transform.rotation.eulerAngles.y, this.angle, 9.3f * Time.deltaTime), 0f);
Vector3 cposition = base.transform.position;
base.transform.position = Vector3.MoveTowards(base.transform.position, cposition, Time.deltaTime * 2f);
this.pos.set(base.transform.position);
this.rot.set(base.transform.rotation);
Okay so if you want to stick with your original idea, you could make it like this:
Code for the bullet
void Update(){
transform.Rotate(degTurnRate, 0, 0);
transform.position += transform.forward * Time.deltaTime * bulletSpeed
}
You can make the code much simple if you just use the Transform.RotateAround
this enables you to rotate the object around the specified targt for the x amount on any of the axes.

Unity3D controlling a ball

In Unity3D, I'm working on simply controlling a ball. My goals are as follows:
Allow the ball to rotate
Move in the direction the ball is facing
Ensure the ball is visually rotating as the ball is moving
What I am attempting to do is almost accomplished with this character controller script I wrote:
transform.Rotate (new Vector3(-Input.GetAxis ("Horizontal") * rotationSpeed, 0, 0));
Vector3 forward = Input.GetAxis ("Vertical") * transform.forward * moveSpeed;
controller.Move (forward);
controller.SimpleMove (Physics.gravity);
My problem with this script is that the ball does not visually "roll".
I attempted to solve this by using a Rigidbody:
float movementHorizontal = Input.GetAxis("Horizontal");
float movementVertical = Input.GetAxis("Vertical");
Vector3 movementVector = new Vector3(movementHorizontal, 0.0f, movementVertical);
GetComponent<Rigidbody>().AddForce(movementVector * moveSpeed * Time.deltaTime);
But in this case, you are unable to rotate anymore then 180 degrees using WAD like the first example. Although it does solve the visual problem.
How is it that I can allow my ball to rotate with the "A" and "D" keys, and then go in the direction the ball is facing with "W" while also visually rotating?
Take a look a this example:
http://unity3d.azurewebsites.net/Labyrinth/
move the ball with the arrow keys
move the camera with the keypad (#5 will chase)
zoom in/out with - + keys
change perspective with V key
The code behind the movement is here:
https://github.com/heldersepu/hs-unity/blob/master/Labyrinth/Assets/Movement.cs

Getting the player jump correctly in Unity3D

I'm still learning the basics so please be a bit gentle. I'm working on the movement script of a roll-a-ball game. I've got the rolling around working perfectly fine, but now I'm trying to add the ability to jump.
In my case the "player" is a basic Sphere. What I'm trying to accomplish is that when space is pressed, the ball leaps into the air in the direction it's currently rolling.
This is what I've tried myself, but gave very strange results:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (Input.GetKey("space"))
{
transform.position += transform.up * 20 * Time.deltaTime;
}
rb.AddForce(movement * speed); //rb = RigidBody
This resulted in the ball sometimes jumping in the wrong direction or simply just speeding into some direction without jumping at all. So obviously I'm getting closer, but not quite there yet. Could anyone explain what I'm doing wrong and how to fix it?
Not looking for a quick fix! Don't learn anything from that.
Have a try at this :)
if (Input.GetKey("space"))
{
rigidbody.AddForce(new Vector3(0, yourJumpForce, 0), ForceMode.Impulse);
}
You also might want to fiddle with the gravity settings in the rigidbody to get your desired jump as well!
EDIT Explanation:
I think it may be because you are applying you movement to your Rigidbody, but your jumping method to the Transform itself.
A Rigidbody is a physics component (see docs.unity3d.com/ScriptReference/Rigidbody.html), where a Transform is to do with position, rotation and scale.
A jump could be considered a physics problem - therefore altering the jump is to do with physics (Rigidbody), not the position (Transform). I tend to do all movement code with the Transform, as you are moving positions, and do all physics based work with the Rigidbody, as you are applying physics to the GameObject! :)
To add to the accepted answer: The jumping into random directions issue could have been caused by the transform.up. That vector is the up direction relative to your current transform. That's usually the same as the actual up direction, except if you rotate the game object. And as the object happens to be a rolling ball in this case, it's very likely that it's rotated.
You can fix it by using the global up direction instead - either by doing it like in the accepted answer or by using Vector3.up.

Clamp player movement correctly

I have the following code to clamp player movement. It works but i have a problem. For example if the player is at position -3.05 and if I hold the button to move left the player still moves over the -3.05 limit to about -3.56. Once i let go of the button it bounces back to -3.05. Same goes for the right side. I do not want it to go over the limits no matter what.
Vector3 tmpPos = transform.position;
tmpPos.x = Mathf.Clamp(tmpPos.x, -3.05f, 3.05f);
transform.position = tmpPos;
The following is the way i add movement to the player:
rigidbody.AddForce (movement * speed * Time.deltaTime);
You should not mix up transform operation with rigidbody unless it's marked isKinematic. So instead of transform.position, try clamping rigidbody.position inside of FixedUpdate.
void FixedUpdate(){
Vector3 pos = rigidbody.position;
pos.x = Mathf.Clamp(pos.x, minX, maxX);
rigidbody.position = pos;
}
However, since you're using AddForce to move your object, a much simpler way is to make empty game objects with box collider on the left and right of the object, which then will limit your object movement like invisible walls.
Try using rigidbody.MovePosition(tmpPos); instead of setting transform.position.
I solved my problem. Instead of using the AddForce to move the object
rigidbody.AddForce (movement * speed * Time.deltaTime);
I use rigidbody.position to move the object. I use Mathf.Clamp to limit the movement before applying it to rigidbody.position.

Categories

Resources