Display Object in front of player without considering its rotation - c#

I can show another object in front of my player using this simple line of code
newPosition =player.transform.position+player.transform.forward * distance
but how can i restrict it to always show in the same position(but in front of player) no matter what is the rotation of my player?

When you rotate gameobject transform.forward always changes.Because of you are using local values. But you need the use word values for this.
newPosition =player.transform.position+Vector3.forward * distance

Related

Unity CharacterController always moves in the same direction independent from rotation

If i'm rotating my player on the Y-Axis and move it like
transform.Translate(Vector3.forward * Time.deltaTime)
it will move along the Z-Axis of the character (as expected).
If I used CharacterController.Move it will always move in the same direction, independent from its rotation.
How can I make the player follow it's rotation?
The problem is that you are translating the character forward on a global level. This means forward will always be in the same direction. Instead, use transform.forward. That way, you are translating the player on the forward vector on the player.
Another way to do it is using transform.rotation * Vector3.forward. This will rotate the global forward by the rotation of the player, returning the same thing as transform.forward.

Roll/rotate the cube on it's edge

How to Roll/rotate the cube on it's edge?(Like this)
I read a couple of articles and answers to the questions but still not what I need.All I learned ,is that I need to create a gameobject in the center of the Cube and 4 others on the pivots.Something like that
And what's next, should I use Quaternions or transform.Rotate?Is the hierarchy correct?
I was about to tell you to use an external 3D software to set the pivot point to the location in your screenshot but it looks like you want to do this with multiple pivot points so you will to use empty GameObjects to accomplish this.
From your screenshot, you seem to be on the right track.
1.Create empty GameObjects and position them in the edges you want to rotate the cube around then place them under the cube.
2.Use transform.RotateAround not transform.Rotate to rotate the cube. The first parameter should be the edge pivot point. The second parameter is the axis you want to rotate the cube against. The third one is the angle.
//cube to rotate
public GameObject cube;
//Assign dge pos from the editor
public Transform edgePivotPoint;
public float rotSpeed = 60f;
void Update()
{
cube.transform.RotateAround(edgePivotPoint.position, Vector3.back, rotSpeed * Time.deltaTime);
}
Note, if rotating the wrong way, try replacing Vector3.up with Vector3.down, Vector3.left, Vector3.right, Vector3.forward and Vector3.back. The one to use depends on your scene setup but trying them will get you to the one

Rotate an object according to terrain in Unity (C#)

I currently have an item placement system for building. It works by instantiating a "ghost" of the object that shows where it can be placed, the ghost (semi-transparent) object is attached to the camera and instantiates the object in its place when the player clicks.
I get the position at which to keep the ghost object like so:
var pos = transform.position + transform.forward * placeDistance; // A position 'someDistance' in front of the player
pos.y = Terrain.activeTerrain.SampleHeight(pos); // Get the position at the surface of the terrain to place the object
firePlaceable.transform.position = pos + Vector3.up * 0.001f; // 'halfHeight' is used in case the pivot is not on the base
Now.. I need the object to rotate according to the terrain so that the fire place is placed more or less correctly rotated. Any ideas? What would the best plan be?
Use the terrain normal vector at the place' position.
For example you could do a raycast straight down from the fireplace. The resulting hit contains a normal that is your place' up vector.
By thinking of it... I assume you already doing a raycast to get the position to place the fireplace right?
Use the placement raycast to get the up vector instead of making a new one.
So basicly do
fireplace.transform.up = clickPlaceHit.normal;

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.

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