- Home /
Problem using a position determined from onTrigggerEnter in Update
I am able to set Y axis of the player with simple transform.position, all within onTriggerEnter method, but the motion is step-wise and jerky. So now I am trying to make the motion smooth by putting the transform.position function in an Update method. However, it seems that the values determined by onTriggerEnter method are not accessible in the Update function. If I print the x and z values to console, they contain expected values from onTriggerEnter function, but appear to be 0 when I print to console from the update function.
Any ideas of what I am doing wrong?
I would never call myself a programmer, so assume the worst :-)
Thanks in advance for any help!
``` using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Step1SetElevation : $$anonymous$$onoBehaviour { private float moveSpeed = 3f; private float currX = 0.0f; private float currZ = 0.0f; private Vector3 currentPos; private GameObject player; private Collider other;
void OnTriggerEnter(Collider other)
{
player = GameObject.FindWithTag("Player");
currentPos = GameObject.Find("PlayerController").transform.position;
currX = currentPos.x;
currZ = currentPos.z;
}
void Update() { player = GameObject.FindWithTag("Player"); player.transform.position = new Vector3(currX, 3.4f, currZ) moveSpeed Time.deltaTime; } } ```
Answer by ZhongshiLiuAndyLaw · Apr 21, 2020 at 06:44 PM
public Transform abc; void OnTriggerEnter(Collider x) { abc=x.gameObject.transform; } //then you can get the axis x y z of abc when you want;
Answer by jvralston · Apr 21, 2020 at 06:58 PM
ZhongshiLiuAndyLaw - Thanks. I just added the code. Can u suggest the better solution?
better solution dipend on what your game want to do,can you explain more of your game?
It not quite a game, more of an 3D experience. When player steps on a square which is on the ground, I want to move player to a specific height, and I want it to be a smooth transition. Does that help?
yes,firstly you use the that simple code i wrote ,to get transform of player,then in update : public Transform specifiHeight; void Update() {abc. transform.position = Vector3.$$anonymous$$oveTowards(abc.transform.position, specificHeight.position, 4f * Time.deltaTime//velocity that you want//);}
// Declarations
public Transform abc;
// OnTriggerEnter
void OnTriggerEnter ()
{
abc=x.gameObject.transform;
}
// Update:
void Update()
{
public Transform specificHeight;
abc. transform.position = Vector3.$$anonymous$$oveTowards(abc.transform.position, specificHeight.position, 4f * Time.deltaTime * moveSpeed);
}
Your answer
Follow this Question
Related Questions
Movement using Time.deltaTime not working on Fast mac 3 Answers
Simple "E" to interact script... 1 Answer
Moving objects performance in PC and Editor 0 Answers
Multiple Colliders malfunctioning 0 Answers