Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Salman_Shh · Apr 18, 2015 at 11:52 PM · c#gameobjectlerppostion

How can I LERP the X position to the X position of the player

with my code i've only gotten the game object to to go directly to the X position of the player - though it moves in a jagged manner

 using UnityEngine;
 using System.Collections;
 
 public class SetPositonToPlayerX : MonoBehaviour {
     private Transform myTarget;  // Using it to grab the player's position
     private float playerXPosition;
     // Use this for initialization
     void Start () {
         
     
     }
     
     // Update is called once per frame
     void Update () {
         // Looks for the "the player" + the transform
 
         if (myTarget == null)
         {
             myTarget = GameObject.Find("The Player").transform;
             Debug.Log("Yo dawg Where'd the Player Ship go!?");
 
             return;// if The TARGET iS NOT FOUND - try again in the next frame.   
         }
         ///////////////////////////////////////////////////////////////////////////////
         if (myTarget != null) {
             playerXPosition = myTarget.position.x;
             transform.position = new Vector2(playerXPosition, transform.position.y);
 
 
         }
     }
 }
 


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 Soos621 · Apr 19, 2015 at 12:13 AM 0
Share

try running it under FixedUpdate ins$$anonymous$$d of update

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Berge · Apr 19, 2015 at 03:18 PM

See the documentation on Vector2.Lerp. http://docs.unity3d.com/ScriptReference/Vector2.Lerp.html

transform.position = Vector2.Lerp(transform.position, new Vector2(playerXPosition, transform.position.y), 0.2);

Comment
Add comment · Show 3 · 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 Salman_Shh · Apr 19, 2015 at 11:43 PM 0
Share

Didn't work :L

avatar image Berge · Apr 20, 2015 at 06:41 AM 0
Share

Can you specify? Did it move at all?

avatar image Salman_Shh · Apr 22, 2015 at 01:44 AM 0
Share

the gameobject doesnt stay in its zposition + moves to the exact X position (moving all jagged-like)

avatar image
0

Answer by Salman_Shh · Apr 22, 2015 at 02:12 AM

 using UnityEngine;
 using System.Collections;
 
 public class SetPositonToPlayerX : MonoBehaviour{
     private float x;
     private float y;
     private float z;
     Vector3 pos;
 
     private Transform myTarget;  // Using it to grab the player's position
     private float playerXPosition;
     public float Smoothing = 0.2f;
     // Use this for initialization
 
 
     // Update is called once per frame
     void Update()
     {
         //the game object's X Position
         //float goXPosition = transform.position.y;
         // Looks for the "the player" + the transform
 
         if (myTarget == null)
         {
             myTarget = GameObject.Find("The Player").transform;
             Debug.Log("Yo dawg Where'd the Player Ship go!?");
 
             return;// if The TARGET iS NOT FOUND - try again in the next frame.   
         }
 
         // if the player target is found
         if (myTarget != null)
         {
             //Updates the Player X Position variable to the target's X position
             playerXPosition = myTarget.position.x;
             setToPositionX();
           
         }
     }
 
     void setToPositionX(){
         //gameObject.transform.position
             //transform.position = new Vector3(Vector3.Lerp(transform.position.x, playerXPosition, Time.deltaTime), transform.position.y), transform.position.z);
         //Sets the x position  float with lerp.
         x = Mathf.Lerp(playerXPosition, transform.position.y, Smoothing);
         //Sets the Y Position to gameobjects's y Position
         y = transform.position.y;
         //sets the Z Position to the gameobject's Z Position.
         z = gameObject.transform.position.z;
         //Puts all the positions together
         pos = new Vector3(x, y, z);
         //Sets the objects position to the new position from "pos"
         transform.position = pos;
 
         }
 }

I found out my own solution - this worked for me I'm able to change the rate in which the gameobject moves towards the target x position by the "smoothing" variable.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to find all GameObjects in a parent in shorter code than I figured out 1 Answer

Shrinking an Object 1 Answer

Distribute terrain in zones 3 Answers

How would you create a vertical endless runner with lerping barriers where the player translates on oblique vectors in C#? 0 Answers

GameObject Pool heaving difficulties getting a pool at all trying to find different aproach 1 Answer


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