Using a Custom rotation in "Instantiate" - Unity 3D - c#

I'am trying to make a throwing spear mechanic in my game.
The problem is that when i instantiate/spawn my "spear" its not facing the right direction i want it to be facing, forward in a 90 degree angle in the X and Y rotation. I tried to give it a rotation like this:
Instantiate(Spear.GetComponent(), transform.position, Quaternion.Euler(0,90,90));
but this only works if the player is in a specific position on the map (look image).
this is the code i have now:
clone = Instantiate(Spear.GetComponent<Rigidbody>(), transform.position,
Quaternion.Euler(0,90,90));
clone.velocity = transform.TransformDirection(Vector3.forward * ThrowForce);

You should give the rotation in the spear prefab and then just normally spawn it based on the prefab rotation like
Instantiate(Spear, position, Spread.rotation)
or if this doesnt work you can look at this
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

Related

In a 3D monogame project, how do I place an object a set distance in front of the camera?

I am working on a monogame project in which the player controls a ship in space (first person) and shoot asteroids that are spawned at random locations with random velocity. When I shoot, I want to place my bullet object 1.5 units directly in front of my camera no matter where the camera is or which direction it is facing. I have tried this line
Vector3 torpedoPos = CameraPosition + CameraDirection * 1.5f;
as these two posts suggest: Placing objects right in front of camera, Placing an object in front of the camera, but as soon as my position is anything other (0, 0, 0) the object is placed in the same location no matter what direction I am facing. In the line of code I posted, torpedoPos is the position I want to spawn my bullet object, CameraPosition is the current position of the camera object in 3d space and CameraDirection is a unit vector with the direction the camera object is facing.
I solved this issue with this line of code
Vector3 torpedoPos = CameraPosition + Vector3.Transform(Vector3.Forward, Orientation * 1.5f);
The orientation is the matrix which holds which direction my ship is facing. By doing a transform with the orientation * 1.5 and Vector3.Forward, I get an amount to add to my current position that will place the torpedo directly in front of the ship.

Get Unity Blend Tree to update based on player rotation (mouse position)

I'm working on the animations for a character in a top down game. I got the animations to work when the player is moving forwards towards the top, however, whenever I rotate the player with the mouse (to aim) the forward animation is now left/right, etc, etc. I'm using Unity3d and C# with blend tree animations.
The code I'm using is straightforward:
animator.SetFloat("VelX", playerInput.Horizontal);
animator.SetFloat("VelY", playerInput.Vertical);
I can't figure out how to make the forward animation play whenever I'm moving TOWARDS the mouse position.
My Solution:
var heading = Input.mousePosition - this.transform.position;
var distance = heading.magnitude;
var dir = heading / distance;
animator.SetFloat("VelX", playerInput.Horizontal * Mathf.Cos(dir));
animator.SetFloat("VelY", playerInput.Vertical * Mathf.Sin(dir));
Any help would be appreciated.
You may have figured this out already but I just had the same issue and I just found out a way to do this. Hopefully it helps others.
Vector3 inputVector = (Vector3.forward * Input.GetAxis("Vertical")) + (Vector3.right * Input.GetAxis("Horizontal"));
Vector3 animationVector = _playerModel.transform.InverseTransformDirection(inputVector);
var VelocityX = animationVector.x;
var VelocityZ = animationVector.z;
this._playerAnimator.SetFloat("x", VelocityX);
this._playerAnimator.SetFloat("z", VelocityZ);
I my case I have my movement script on a parent empty object. This object rotates the child player model, which is the one getting animated. If you do this then you will need to reference the player model like I am doing. Otherwise you can you transform if you are using the parent object.
I am using a character controller on my parent object not a rigidbody, but it may work the same either way.
From my understanding the key part is 'InverseTransformDirection' which is taking my physical world horizontal and vertical vectors and then converting them based on the rotation of my player model. This then makes the movements be relative to the players rotation rather than the worlds rotation and I can move backwards or strafe correctly as I am moving my mouse for rotation and wasd for movement.

Rotating 2D world within Unity

I am looking at developing a game within Unity by using C#
The game will take the gyroscope orientation and rotate accordingly to the direction of which the phone is rotated.
I am curious of how this would work, I understand how I would be able to read and update the gyroscope orientation however I am unsure on how to assign this to a world to rotate. There will be a player on world which will be the next challenge to prevent the player clipping through the world when it is rotated.
Hence the world should rotate around the players current location.
I currently have no code as I am in the process of designing this, however i am unable to get the logic of how to make this work within my head
Thankyou
If I get your objective right, I guess rotating the camera like in a Fist-Person game should be enough, and would be way simpler than rotating the whole world while keeping the player static.
This is how I would go about it, first I wouldn't rotate the world, as that is alot of objects to rotate, but if you really wanted to you could parent all of the world to a single game object, then rotate that object about an axis based off of the players position(See this: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html).
The simplest way is to attach your main camera to your player(Drag and drop it on your Player Object in your hierchy), then rotate your player.
for an example you can test on your machine without the gyroscrope:
using UnityEngine;
public class rotatorScript: MonoBehaviour {
void Update() {
// Will Rotate based off of left/right arrows
float rotator = -Input.GetAxisRaw("Horizontal");
// Move up or down based off of up/down Arrows
float verticalDirection = Input.GetAxisRaw("Vertical");
// Do the actual movement... using space.self so it is based on the player not the world.
transform.Translate(Vector3.up * verticalDirection * Time.deltaTime * 5f, Space.self);
transform.Rotate(0f,0f,90f * Time.deltaTime * rotator);
}
}
This script would be attached to the player.

Rotate camera in function of direction of ball

I'm making a maze whit Unity3D where a ball can roll and find the out way. Because the ball can be hide after a wall, I want to rotate the camera to a better position in function of the direction of the ball.
Take this example: the ball is rolling in to you (in the direction of the black arrow). So you can see or beter can't see, is where the ball is rolling to. So the camera must turn to the other side of the ball. If the ball rolls away from you must the camera turn to the original location.
The problem is now, I know how I can replace the camera but not in function of the direction? Can anyone help me with this? I'm just starting with Unity3D. Language behind I use C#.
Here is another situation where it is better to rotate the camera. (up: is scene, below game mode).
You can use this to set the camera position behind the ball based on the velocity and then the rotation in the direction of the ball
Vector3 offset = new Vector3(1,1,0);
transform.position = ball.transform.position - ball.GetComponent<Rigidbody>().velocity / ball.GetComponent<Rigidbody>().velocity.magnitude + offset;
transform.LookAt (ball.transform.position);

How can I rotate an object that is falling and pulled?

I have balls falling (gravity) in my game, and in its Update method I've also added a slight pull, as the wind was blowing:
transform.Translate(new Vector3(sidewaysDrag,0));
Now, I want to rotate my GameOBject around the Z axis as well (its a 2D game), ive tried:
transform.Rotate(Vector3.forward * Time.deltaTime * 200);
But I guess my anchor is wrong, because instead of rotating around itself the whole object is rotating in a very big curve. The rotation is correct if I comment out the translation...
Put your ball object under another empty object(parent).
And translate parent, rotate ball.
or translate ball's world position, rotate ball's localRotation
It's because you are using Vector3.forward, which actually points to Z-axis in world space, you need Z-axis in local space, you can get that by doing transform.TransformDirection(Vector3.forward);

Categories

Resources