Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jvralston · Apr 21, 2020 at 06:29 PM · movementupdateontriggerenter

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jvralston · Apr 21, 2020 at 06:56 PM 0
Share

``` 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; } } ```

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jvralston · Apr 21, 2020 at 06:58 PM

ZhongshiLiuAndyLaw - Thanks. I just added the code. Can u suggest the better solution?

Comment
Add comment · Show 20 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZhongshiLiuAndyLaw · Apr 21, 2020 at 07:08 PM 0
Share

better solution dipend on what your game want to do,can you explain more of your game?

avatar image jvralston · Apr 21, 2020 at 07:34 PM 0
Share

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?

avatar image ZhongshiLiuAndyLaw jvralston · Apr 22, 2020 at 09:18 AM 0
Share

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//);}

avatar image jvralston ZhongshiLiuAndyLaw · Apr 22, 2020 at 12:08 PM 0
Share

// 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);
 }

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement using Time.deltaTime not working on Fast mac 3 Answers

Simple "E" to interact script... 1 Answer

so how to OnTriggerEnter2D(Collider2D collision) work update so game component if player do not enter collision so game component enabled again 1 Answer

Moving objects performance in PC and Editor 0 Answers

Multiple Colliders malfunctioning 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges