I want to place the camera in front of a GameObject onTrigger. It works with my code as long as the GameObject faces one particular direction, but if I rotate it, the target position will be behind the GameObject.
What do I miss? :)
mainCamera.transform.position = currentGameObject.transform.position + new Vector3(lookingDistance, 0, 0);
mainCamera.transform.LookAt(currentGameObject.transform);
The script is not attached to the GOs. I use kind of an interactionManager.
Your rotation code seems all right, but when finding new position you do not take rotation into account.
Try changing
+ new Vector3(lookingDistance, 0, 0);
into
+ currentGameObject.transform.TransformPoint(new Vector3(lookingDistance, 0, 0));
Related
I am VERY new to unity and I wrote this very basic program to move the player. This program works when there is no conditional statement on if the w key is pressed, but if you add it, the capsule is unable to move.
if (Input.GetKeyDown("w"))
{
Vector3 cameraForward = Camera.main.transform.forward;
cameraForward.y = 0;
cameraForward = cameraForward.normalized;
Vector3 moveDirection = cameraForward * speed * Time.deltaTime;
transform.position += moveDirection;
}
The only issue I can see with this is that the capsule is clipping into the plain, although I have a collider and a rigidbody on it. If you run this the capsule just does not move, at all. If it at all matters, I also have another line of code that sets the rotation of the capsule to 0, 0, 0, 0 every frame.
Input.GetKeyDown() only triggers once per key-press. Your biggest issue (there may be others) is that that will only move the player forward for a single tick. You want Input.GetKey() (Unity Docs) instead.
I created a simple shooter in Unity and i want to change the position of my Player (FPSController). I can only change the position when i do it in the Prefab but this is useless for me.
If i call functions like "InstantiatePlayer()" where it tries to change the position, the player will be teleported after 0.1 sec back to the old position.
public void InstantiatePlayer()
{
GameObject temp = Instantiate(PlayingplayerPref);
temp.transform.position = new Vector3(31, 6, 7);
}
vgro's solution is ok, but instead of creating a new Quaternion you should use Quaternion.identity, won't do much of a difference here but it's good practice
You can specify the position when on instantiating.
Instantiate(Object PlayingplayerPref, Vector3 position, Quaternion rotation);
If you dont want to specify the rotation just set the quaternion to (0,0,0,0), so you will have
Instantiate(PlayingplayerPref, position, new Quaternion(0,0,0,0);
Hope this helps :)
I'm recreating a movement system from dragon quest heroes rocket slime on the DS. In that game, the player can hold down the A button and stretch their slime main character in a direction. Letting go of A while it's stretched would sling them off in that direction.
Here's a gif of what it looks like:
Notice how he bounces off the walls diagonally. My replication currently looks like this (ignore the buggy animations :P)
It looks good! However, if I were to hit a wall diagonally, I would just move backwards in the opposite direction rather than reflecting off it.
Here's the code for how I handle bouncing off walls.
IEnumerator wallBounce(Vector2 hitVelocity)
{
playerAnimator.enabled = true;
yield return new WaitForSeconds(.3f);
playerAnimator.CrossFade("Walking " + curDirection, 0); // place holder animation
pVelocity = Vector2.Scale(-pVelocity,new Vector2(0.25f,0.25f));
endPos = transform.position + new Vector3(pVelocity.x, pVelocity.y);
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
curState = state.blasting;
transform.position = Vector3.MoveTowards(transform.position, endPos, 5f * Time.deltaTime);
}
I know about the Vector3.Reflect method in Unity but I'm not really sure how to implement it into my current code.
I seem to have done it.
I get the reflection vector like this:
Vector3 reflectedVelocity = Vector3.Reflect(pVelocity*.5f, foundHit.normal);
And then pass it to wallBounce as the hitVelocity.
StartCoroutine(wallBounce(reflectedVelocity));
I then changed this line:
endPos = transform.position + new Vector3(pVelocity.x, pVelocity.y);
into
endPos = transform.position + hitVelocity;
I wanted to lerp rotation to new Quaternion(0,0,0,0); but it seems like not animating to desired rotation at all... It just stop at where it is...
What I tried were those three on below
RectTransform rectTransformComponent = this.transform.GetComponent<RectTransform>();
rectTransformComponent.localRotation = Quaternion.Lerp(rectTransformComponent.localRotation, new Quaternion(0,0,0,0), 0.1f);
and
this.transform.localRotation = Quaternion.Lerp(this.transform.localRotation, new Quaternion(0,0,0,0), 0.1f);
and
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, new Quaternion(0, 0, 0, 0), 0.1f);
I'm using RectTransform for this object. It was working without lerping. It's in the Update() so it will be looped every frame. I tried to bring up the speed for lerp but no luck...
Does someone have any idea what is going on??
this.transform.localRotation = new Quaternion(0,0,0,0) //This works
An all-zero quaternion (new Quaternion(0,0,0,0)) is an invalid rotation. Quaternion.Lerp doesn't know how to interpolate to an invalid rotation.
To rotate to an Euler rotation of x=0,y=0,z=0, then you should instead use Quaternion.identity (which is equivalent to new Quaternion(0,0,0,1)).
I am trying to rotate an object around it's local axis (In this case a humanoid index finger) with my custom editor.
public float amount;
void OnGUI() {
amount = EditorGUILayout.Slider("Rotate Amount", amount, 0, 100);
index1.transform.localEulerAngles = new Vector3(0, 0, amount);
}
The issue I am having is when I move the slider, the finger rotates down and forward so the tips of the fingers point outwards, when in theory they should point towards the elbow.
I think I am using the wrong type of transform here, so what should I do to use the right transform?
Try to use:
index1.transform.Rotate(new Vector3(0, 0, amount), Space.World);
or
index1.transform.Rotate(new Vector3(0, 0, amount), Space.Self);
Hope it solves your problem :)
There may be 2 parts to this:
Getting the current rotation of the object on the local z-axis, here's one way to do that. https://stackoverflow.com/a/47841408/228738
Using the current rotation value to find the amount of rotation to apply to the object.
code for step #2
var currentZEuler = zRotation(this.transform.rotation).eulerAngles.z;
var deltaZEuler = amount - deltaZEuler;
index1.transform.Rotate(0, 0, deltaZEuler, Space.Self);