Unity rigidbodies movePosition over network - unity3d

I think this is a network setting problem,
I am having some issues with my game in unity and I have been at it for several days. I am using rigidbody.movePosition to have a object follow a vr controller. If I hold down a button I start to move and I can swing the object with my arm. everything works perfect in regular testing. However, once I tried to use networking the rigidbody freezes once I push the button. when i stop pressing it snaps back to place.
couple notes. my rigidbody is in a seperate empty. kinematic is on. My problem is fixed if i turn off kinematic but the object floats away.
private Rigidbody r;
public SteamVR_TrackedObject controller;
void Start () {
r.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
r.MovePosition (controller.transform.position);
r.MoveRotation (controller.transform.rotation);
}

Related

The "OnCollisionEnter" function isn't being triggered in one case but is being triggered in another case

I'm making a virtual reality application for the oculus platform with the unity engine & experiencing a problem in the process. For some reason, when i'm holding two objects (a hammer & a nail) with my controllers, between the two of them, the "OnCollisionEnter" function isn't being triggered, even though it's being triggered between any one of them & the target surface. Can someone please help me understand why it's happening? Is there a solution to it?
First make sure that isTrigger is disabled.
Then add Rigidbody to both objects.
Also be sure to use rigidbody instead of transform to move and change position.
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
rb.AddForce("some force vector");
rb.MovePosition("some move vector");
}
It seems that this is a recurring & unresolved issue related to how VR works with the unity engine.

How to make a platformer in Unity?

(I am a new person in stackoverflow, might ask a bit different)
How to make a platformer in Unity?
Unity 2020.2.0a11.1312.3
Info before start: I want to make a mobile game.
(with some buttons)
Hello, I want to make a platformer in Unity.
I tried to use Rigidbody2D.AddForce() method but I couldn't get it working.
Can somebody help me?
I don't usually work on mobile games in Unity but these are generally the reasons why rigidbodies don't work.
Make sure to connect the script to the object with the rigidbody by dragging the script on to the object.
Use RigidBody2D Rigidbody2D = GetComponent<RigidBody2D>() at the start of your script.
If you're using the AddForce() method in the Start() function then you need to move it to the FixedUpdate() function.
Sometimes the movement does work but it doesn't move the object very fast because there isn't enough force so you might have to increase the force.
Check to make sure that there are no movement constraints applied to the Rigidbody.
Since you're working with buttons you will have to link the buttons to the script with the rigidbody like:
public void OnPointerDown(PointerEventData eventData)
{
//Start moving logic
moving = true
}
public void OnPointerUp(PointerEventData eventData)
{
//Stop moving logic
moving = false
}
private void FixedUpdate(){
if(moving){
//Moving
Rigidbody2D.AddForce(force * Time.deltaTime)
}else{
//Not moving
}
}
Since OnPointerUp/Down is only called once when the event happens you need to put your start moving logic in OnPointerDown and stop moving the object in OnPointerUp.

OnMouseOver works differently on same objects

I am a beginner in Unity and I just found out a behaviour I do not understand...
I have a prefab "cell" that I made from a sprite and I want it to change its color when my mouse is over it.
So I added a BoxCollider2D component to it as well as the following script:
public class Cell : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
private void OnMouseOver()
{
GetComponent<SpriteRenderer>().color = Color.magenta;
}
private void OnMouseExit()
{
GetComponent<SpriteRenderer>().color = Color.black;
}
}
Then when I drag and drop the "cell" prefab to the scene, it won't work (when my mouse is over the cell, nothing happens).
Same problem when I add another "cell" prefab to the scene.
But when I add a third "cell" prefab to the scene, the feature works on the 2 first cells but not on the third.
I probably missed something or there is a behaviour I do not know, anyway if someone knows why this is happening, please tell me.
Thanks!
I just tested your code in my game using a 2d box sprite and it works fine.
Video > https://youtu.be/6GP3-aV9g3g
You may want to try a couple of things to debug it.
First make sure that there is a BoxCollider2D and Rigidbody2D attached.
Make sure that there is nothing covering the boxes in the scene.
When I am having trouble with an aspect of the game I try to break it down into its simplest components. Try making a scene with nothing in it apart from the box and trying it, if that doesent work, try attaching the scrip to a non prefab object.
Try adding Debug.Log("Mouse Enter"); to the subs to check if the mouse is detected on enter, if it is detecting the mouse, maybe your spite renderer isn't working properly.
Try these things and let me know if they don't work, I would be happy to keep trying to figure it out.

There is no reason for this Unity script to make the sprite move, yet it moves

Here's the code:
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float strength = 100f;
void Start () {
//Initialize the body of the sprite so that forces
//can be applied.
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
//Note2Self: var says this is a variable of unspecified type.
var touch = new Touch();
/*
if (touch.phase == TouchPhase.Began){
rb.AddForce(transform.forward * strength);
}*/
if (Input.anyKey)
rb.position.Set(0, 100);
}
}
I was trying to practice some basic stuff in Unity (I am not used to programming in IDE's at all, we've just used vim in my program so far) when I happened upon this oddity.
First, I didn't understand why the sprite would move at all when there can't be a touch identification, since I haven't actually tested this on a mobile device. So I commented it out and for some reason the sprite still moves. That code shouldn't be doing anything, yet it is.
I have checked to see if the sprite is using the up-to-date script - it is - and I have checked if the script is targeting the correct rigid body and that it is a rigidbody2D. It is.
What is going on?
If it is just falling it is probably effected by gravity.
You can turn this off in your script by adding rb.gravityScale = 0; at the end of your Start() function
OR
by setting it in the editor inside the rigid body component
I looked in the unity documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
which says that applying a Rigidbody2D component to an object will place it under control of the physics engine.
The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces.
I have run into issues with rigidbodies on more than one occasion, I suggest you check the RigidBody2D component in the unity inspector window and make sure to uncheck use gravity.
Also, you may want to just write a custom script without using a rigidbody. Doing a search on youtube will probably give you exactly what you need for that. Hope this helps!

Particle at Input.mousePosition Point

I am trying to understand this link which, I believe, is supposed to create a particle where you left-click your mouse. Am I correct?
I have attached the script to an empty game object, so it runs when I run my Unity project. I added a new Particle System game object and dragged it onto the public field of the inspector. Is this the right way of doing it, or should I assign something else to the public variable?
The particle system starts firing up as soon as I run the project, so how do I stop that and then make it begin when and where I have clicked my mouse curser?
public GameObject particle;
if ( Input.GetButtonDown("Fire1") )
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast(ray) )
Instantiate(particle, transform.position, transform.rotation);
Debug.Log(Input.mousePosition.x);
}
You should either turn off looping in the particle system or Stop() or Pause() it on Awake() in your script and Play() when clicking the fire button.
Check this page: http://docs.unity3d.com/ScriptReference/ParticleSystem.html
The sample that you have provided doesn't take a particle system, but some 'projectile' prefab, it basically spawns the prefab instance when you click mouse button. It is just an example of very very basic shooting mechanic. I believe that naming on that script could have been better. (I believe that this sample was taken from one of Unity's Merry Fragmas videos, they used some kind of particle system in a similar manner, you'll have to configure the particle system to burst a bunch of particles and not loop after that if you want to use it this way)