My gameobject is a rectangle. It has a rigidbody, and a boxcollider. The anchor point is toward the left end of the gameobject. Using this code while the game is running causes the gameobject to turn around the z axis on the anchor point. when i set istrigger false, the object begins spinning around the center of itself. why does this happen? Is there a way to make the gameobject to spin around its anchor point while the istrigger bool of the box collider is false?
After some playing around, i have figured out that it i rotating on the center of the boxcollider. I need to figure out how to change the center of the box collider now without affecting the shape
private void FixedUpdate()
{
rigidbody.AddTorque(Vector3.back);
}
rigidbody.centerOfMass = gameobject.transform.position;
Related
I have got a sphere that moves in the world space with respect to the camera. I'm clamping an image to the sphere so that it moves together with the sphere. I'm using the following code to do so. This is attached to the sphere gameobject.
public class ClampImage : MonoBehaviour
{
public Camera FirstpersonCamera;
public GameObject image;
void Update()
{
//get the position of the sphere in the worldspace
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
//assign the world position of the sphere to the image
image.transform.position = spherePosition;
}
}
I tried to use the same code to clamp the Canvas itself to the sphere but it is not being clamped to the sphere. How do I clamp the entire canvas to the sphere?
Update:
entire canvas refers to the canvas together with its child Gameobjects like texts and images.
The canvas is within the PlayerGameobject. The following are the settings for the canvas and the player Gameobject.
Canvas setting:
Player Gameobject setting:
I have registered the player in place of the image. It just vanishes completely when I ran the code.
If you group the UI items you want in a panel, you can move the panel by placing it in your "image" variable as thats a GameObject, and then you can move the panel as required and all objects will move with it
So I am making my first 3D video game in Unity3D. I want this player gameobject with a rigidbody and a capsule collider to have collision with the environment ie the player should not be clipping through walls. I've attached images of the player's components and properties.
The player has two colliders: the box collider, and the capsule collider. The box collider is disabled as of right now because if I activate the box collider instead of the capsule collider, there seems to be no collision. Aside from that, the current capsule collider looks like so.
(It is the one light green line through the center of the player model)
In the scene, there is a house next to the player to which the player should collide with the walls of the house instead of clipping through. The house has 3 different box colliders on 3 sides of the rectangle building shown below. There is no collider on the far side of the image.
Inspector for the box colliders on the house
It is important to note that there is no rigidbody component on the house and it is set as a static game object.
Here is a gif of the issue I am talking about with the above settings. From that gif it is visible that there is collision on the z-axis of the house(both sides) but there is absolutely nothing on the x-axis as I am able to clip through the house and move inside.
Initially I thought the fact that the radius of the main player gameobject's capsule collider is 0 could be the issue however there seems to only be collision with the radius set to 0(at least with the collision script I am using which is attached below). As a result, I increased the radius to where it encompassed the entire player object. Here is an image with the inspector properties of the collider included as well.
Here is the gif in play mode. From the gif any collision that was there before is completely removed and the player can now clip on all sides.
At the point, I thought it could be because I 3 different box colliders on the house and using only 1 would solve the issue. As a result, I changed the house's box collider to look like so:
Even with this house box collider, the player was able to clip through all sides of the house with both settings of the capsule collider(0 radius and radius that encompasses the player's body).
The box collider yielded the same results.
Here is my collision script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour
{
public bool sendTriggerMessage = false;
public LayerMask layerMask = -1; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private Rigidbody myRigidbody;
private Collider myCollider;
//initialize values
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
myCollider = GetComponent<Collider>();
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(myCollider.bounds.extents.x, myCollider.bounds.extents.y), myCollider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = myRigidbody.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
{
if (!hitInfo.collider)
return;
if (hitInfo.collider.isTrigger)
hitInfo.collider.SendMessage("OnTriggerEnter", myCollider);
if (!hitInfo.collider.isTrigger)
myRigidbody.position = hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent;
}
}
previousPosition = myRigidbody.position;
}
}
I am using this free animation pack to animate the player.
Not sure if this is a Unity bug because the weird thing is that the collisions work for certain houses but not for others. Sometimes theres collision on all axes and sides with a single box collider and sometimes with two different box colliders and sometimes nothing works and there is only collision on one axis.
<IsKinematic>
"Controls whether physics affects the rigidbody.
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore..."
Deselect that in the RigidBody and the collider will work.
I'm developing an enemy AI in a 2D Game that I'm working on. This enemy swims and I wanted to make a "floating effect" animation for the enemy, so I made an animation where the Y Axis of the game object bounces up and down.
I Use transform.Translate() to move the enemies in the game and it worked just fine until I made this animation. But, when the animation is playing, the enemy can't move in any direction.
public virtual void Move(float speed)
{
if (canMove)
{
transform.Translate(new Vector2(speed, 0) * Time.deltaTime);
}
}
Once you have a keyframe in any state of your animator for a certain property the animator will always overrule any changes done in a script because the animation updates are all done after Update. You could try to either move your code to LateUpdate.
Or in your specific case you do not want the x component of your position keyframed at all. Simply remove all the keyframes for the x (and z) component(s) of the position from the animations so only y has keyframes. This should solve your problem.
Alternatively use your movement script on a GameObject on a higher level in the hierachy as your Animator - meaning add a new GameObject, make the animated object a child of it and place your movement script instead on that parant object.
I have made a thing in unity2d where an object moves according to the location of the mouse compared to the location of the object, to do this there is a vector going from the object to the mouse, all movement is either parrallel to this or perpendicular to it.
This worked fine before adding rotation of the sprite (added as shown below)
void RoterModMus()
{
fRotationIGrader = Vector2.Angle (Vector2.up, vVectorToMouse.normalized);
if(vVectorToMouse.normalized.x<0)
{
transform.rotation =Quaternion.Euler(0, 0, fRotationIGrader);
}
else{transform.rotation =Quaternion.Euler(0, 0, -fRotationIGrader); }
}
When i stop running this funtion in update the movement works again.
Is there a way to either rotate the sprite without rotating the object, or to make this not hurt movement??
I havn't been able to find anything on any of those questions, and i cant figure it out (sorry for danish code)
The code checks the angle between up and the mouse (up is zero in unity) and sets the rotation of the object to that or minus that
Awwkaw
One solution is to add an empty gameObject with the sprite component as a child to your object and then rotate the child AKA empty gameObject with sprite.
I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.