Why is this translate not working?

I am new to unity and I’m having some trouble. I am using the steamvr recourse from the asset store, and I have platforms that move across the game. I am trying to move the player along with the platforms when he is above them. I am detecting when the player is above the platforms but the player isn’t moving. I have made the player a child of a Game Object and when I move the game object I move the whole player. The Game Object is referenced as “player” in my script. I think the translate isn’t working.

This code is in the platforms, Which have their own trigger.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformPlayerScript : MonoBehaviour
{
    public GameObject player;
    public Vector3 lastPos;
    public bool inside;
    void Start()
    {
        lastPos = transform.position;

    }

    void OnTriggerEnter(Collider other)
    {
        inside = true;

    }

    void OnTriggerExit(Collider other)
    {
        inside = false;
    }

    void Update()
    {
        if (inside)
        {
            Debug.Log("move3");
            player.transform.Translate(Vector3.right * 2f * Time.deltaTime);
            lastPos = transform.position;
        }
    }
}

Is the debug log outputting? If not, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Also, animations can override code translate commands. Animations always “win” so check there isn’t an animation driving this object.

Also, there are specific requirements for OnTrigger/OnCollision type calls to work. Look in the docs and make sure you are meeting 100% of those requirements, otherwise it won’t work.

The debug is outputting. the player just won’t move.
Also, would a animation in a child of the object stop it form moving?

If an animation drives a property of a child, it will always win. Either disable the animation, or remove it. At the minimum you can start the game, pause the editor, disable the animation, and check instantly if that’s what’s going on, then decide how to proceed.

Also, as a note, your line 32 and line 33 are NOT transacting against the same transform, unless this script is on the player. This might not be what you intend.

Yeah that’s what I wanted. Is my transform line all correct? Something I do see when I press start is the game object moving around the scene, BUT the position doesn’t say its changing, and the player isn’t moving with it.

Did you try printing the value of player.transform.position before and after the translate? At this point like I noted above, print EVERYTHING… nobody here is going to be able to see what’s going wrong.