Unity general questions

Comments

  • edited
    @Thelangfordian the problem isn't in doing barrel rolls, my current setup already allows for that. The problem I'm trying to solve for is to correct for rolling when my controls are in neutral - so a self-correcting force is what I'm looking for, not for the player to be able to counter the rotation manually. Does that make sense?

    My code that I attached already does do that, but it also has some unforeseen weird vector force in there that makes it accelerate in odd directions when it shouldn't (it should just be purely rotational, no acceleration).

    So how do you self-correct a torque in one axis? I think answering that alone will do.
  • edited
    @Tuism, Ah ok. I see.

    To reduce the torque you would need to add a counter torque (with dampingFactor) until the rigidbody.angularVelocity is zero. To apply this to a single axis you need to convert the angularVelocity to local coordinates, apply dampingFactor, then convert back to global coordinates.

    I tried the following, seems to work. :/

    if(noUserInput) {
                if(rigidbody.angularVelocity != Vector3.zero) {
    
                    Vector3 localAngularVelocity = transform.InverseTransformVector(rigidbody.angularVelocity);
    
                    // Apply damping factor to desired local axis
                    localAngularVelocity.y *= -0.85f;
                    localAngularVelocity.x = 0f;
                    localAngularVelocity.z = 0f;
    
                    Vector3 globalAngularVelocity = transform.TransformVector(localAngularVelocity);
    
                    myRigidbody.AddTorque(globalAngularVelocity);
                }
            }
  • This is a very, very, very basic question, but I don't even know what to google :P

    If I want to make some script/function that I can use in another script by just including it into a script with "using" at the top, how do I do that? Is that a good way of making repeat-use functions?
  • edited
    You need to get an instance of the class that you want to call a function from with GetComponent, so you could have a script attached to your camera and then call Camera.main.GetComponent<MyScript>().MyFunction(). The other way is to declare the function in a class as public static, then you can call it from anywhere with MyClass.MyStaticFunction(), but you can't access instance specific variables or functions from that function. If you just want to use declarations from another namespace, you would use using.

    Thanked by 1Tuism
  • edited
    A question that's probably damn easy but I don't know what to google for :/

    I have this code to check if a point is within in the camera view. Sounds good:

    public static bool InfiniteCameraCanSeePoint (Camera camera, Vector3 point) 
    	{
    		Vector3 viewportPoint = camera.WorldToViewportPoint(point);
    		return (viewportPoint.z > 0  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
    	}


    But I get this error and I'm not sure why:
    Assets/Scripts/Func.cs(63,29): error CS0119: Expression denotes a `value', where a `method group' was expected
    The "Contains" is highlighted in monodevelop.

    Help? Thanks!
  • This part doesn't look right. Something missing after the 0 but I'm not too sure what you want to be there.
    return (viewportPoint.z > 0  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));


    Probably this?
    return (viewportPoint.z > 0 &&  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
    Thanked by 1Tuism
  • Zaphire said:
    This part doesn't look right. Something missing after the 0 but I'm not too sure what you want to be there.
    return (viewportPoint.z > 0  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));


    Probably this?
    return (viewportPoint.z > 0 &&  (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
    Thanks for this correction, the code runs, but it doesn't seem to work as intended.

    The two lines together are supposed to return whether a point in world space is within the camera's view.

    I don't understand the syntax of the second line of code at all, is it possible to explain it to me? (I have literally no idea what to google for :/ )
  • I don't think that code will work for 3d objects because the new Rect only checks for objects within 0 to 1 ranges where the 3d objects might be X = 200.

    Have a look at this link as I think it will work for what you are trying to do.
    https://docs.unity3d.com/ScriptReference/GeometryUtility.TestPlanesAABB.html
    Thanked by 1critic
  • At a glance, the code doesn't look wrong.

    camera.WorldToViewportPoint should give you a point in your viewport space, where bottom left is (0,0), top right is (1,1), and the z coordinate is the distance in front of you. So when you're checking if something's within the camera view, you're checking if the z is positive (i.e. it's in front of your camera rather than behind it), and that the x and y each fall between 0 and 1.

    I'd debug it by printing out what viewportPoint is giving you, and seeing why it is or isn't falling within what you expect.
    Thanked by 1Tuism
  • Code works fine for me, maybe you are not giving it the right camera or the right position?

    image

    using UnityEngine;
    using UnityEngine.UI;
    
    public class cube : MonoBehaviour {
    
        public RawImage indicator;
    
    	// Use this for initialization
    	void Start () {
    		
    	}
    	
    	// Update is called once per frame
    	void Update ()
        {
            if (InfiniteCameraCanSeePoint(Camera.main, this.transform.position))
                this.indicator.color = new Color32(19, 176, 66, 255);
            else
                this.indicator.color = new Color32(197, 23, 23, 255);
        }
    
        public static bool InfiniteCameraCanSeePoint(Camera camera, Vector3 point)
        {
            Vector3 viewportPoint = camera.WorldToViewportPoint(point);
            return (viewportPoint.z > 0 && (new Rect(0, 0, 1, 1)).Contains(viewportPoint));
        }
    }
    Thanked by 1Tuism
  • Rad, thanks for trying it out, I'll check if I've fucked up on the implementation a bit later, busy with something else now :)
    Thanked by 1critic
  • What I can see might need to be added to @critic example is to factor in half the width/height of the object as it only triggers if the centre of the object is within view of the camera
    Thanked by 1critic
Sign In or Register to comment.