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 Jake33011 · May 02, 2018 at 10:34 PM · movementpositionplayerchangedefault

My player returns to default position when pressing play

Hey guys I have a problem. I already looked for an answer from someone else who may have had the same problem but I couldn’t find one. So this is what happens : I wrote a script to make my ball (Player) move always up and go on one side or the other using the left and right keys and it works fine. The problem is that I want my ball to have an original position of something like (-1.3, 0, 0) but when I hit play the ball returns to 0 on the x axis while going up and I can’t figure out why. I had kind of the same problem for my camera-following-player script but then I could resolve it just by setting the transform.position.x to 0, and can’t do that for player otherwise it won’t go right or left anymore. By the way there is no animation on the ball and I just have a character controller in addition to the script on the player. So please if anyone knows how to resolve that problem it would be great thanks :)

This is the script on the player :

 private const float TUBE_DISTANCE = 1.5f;        
 private CharacterController controller;
 private float verticalVelocity;
 private float speed = 2.0f;
 private int desiredTube = 1; // 0 = Left, 1 = Middle, 2 = Right

 private void Start()
 {
     controller = GetComponent<CharacterController>();
     transform.position = new Vector3(-1.3, 0, 0);
 }

 // Update is called once per frame
 private void Update()
 {
     // Gather the inputs in which tube we should be
     if (Input.GetKeyDown(KeyCode.LeftArrow))
         MoveTube(false);
     if (Input.GetKeyDown(KeyCode.RightArrow))
         MoveTube(true);

     // Calculate where we should be in the future
     Vector3 targetPosition = transform.position.y * Vector3.up;
     if (desiredTube == 0)
         targetPosition += Vector3.left * TUBE_DISTANCE;
     else if (desiredTube == 2)
         targetPosition += Vector3.right * TUBE_DISTANCE;

     // Let's calculate our move delta
     Vector3 moveVector = Vector3.zero;
     moveVector.x = (targetPosition - transform.position).normalized.x * speed;
     moveVector.y = speed;

     // Move the ball
     controller.Move(moveVector * Time.deltaTime);

 }

 private void MoveTube(bool goingRight)
 {
     desiredTube += (goingRight) ? 1 : -1;
     desiredTube = Mathf.Clamp(desiredTube, 0, 2);
 }

And this is the script on the camera :

 public Transform lookAt; // Our ball // object we're looking at
 public Vector3 offset = new Vector3(0, 1.29f, -34);

 private void Start()
 {
     transform.position = lookAt.position + offset;
 }

 private void LateUpdate()
 {
     Vector3 desiredPosition = lookAt.position + offset;
     desiredPosition.x = 0;
     transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Txandy · May 03, 2018 at 02:18 PM

In line 30, you don't have condition, so Update function put the player to Vector3.zero position

 Vector3 moveVector = Vector3.zero; // You set here to 0
      moveVector.x = (targetPosition - transform.position).normalized.x * speed;
      moveVector.y = speed; // Speed = 0
      // Move the ball
      controller.Move(moveVector * Time.deltaTime); // Move to 0,0,0

moveVector its 0,0,0

You need to put this in one condition.

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

142 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

Related Questions

Lock player position 1 Answer

How do you change a players position during an animation? 2 Answers

Camera rotation around player while following. 6 Answers

Change player's y-position for a specific animation 1 Answer

Player GameObject changes position when I start the game 4 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