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 /
  • Help Room /
avatar image
0
Question by fladrifbirch · Jul 17, 2020 at 04:19 AM · transformtranslateteleport

Trying to move the Player 30 units on the x axis back and forth when clicking but transform.Translate not working reliably?

I've been trying to learn Unity and followed some tutorials and have been reading the documentation and I'm trying to teleport the Player when you click, it works a lot of the time but not all the time, sometimes you have to click a few times for it to actually happen. Any ideas what I could do to fix this?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor.Experimental.GraphView;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController controller;
 
     public Transform groundCheck;
     public float groundDistance;
 
     public float jumpHeight = 3f;
 
     public LayerMask groundMask;
 
     public float speed = 12f;
     public float gravity = -9.81f;
 
     Vector3 velocity;
     Vector3 moveRightVector = new Vector3(30f, 0f, 0f);
     Vector3 moveLeftVector = new Vector3(-30f, 0f, 0f);
     Vector3 playerStop = new Vector3(0f, 0f, 0f);
 
     bool isGrounded;
 
     private void GravityEffect()
     {
         float localGravity = gravity;
 
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         controller.Move(velocity * Time.deltaTime);
         velocity.y += localGravity * Time.deltaTime;
 
         if (isGrounded && velocity.y < 0)
         {
             velocity.y = -2f;
         }
     }
     private void PlayerMove()
     {
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
 
         Vector3 move = transform.right * x + transform.forward * z;
 
         controller.Move(move * speed * Time.deltaTime);
     }
 
     private void PlayerJump()
     {
         float localGravity = gravity;
 
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         controller.Move(velocity * Time.deltaTime);
 
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * localGravity);
         }
     }
 
     private void PlayerTeleport()
     {
         if (transform.position.x < 14)
         {
             transform.Translate(moveRightVector, Space.World);
         } else if (transform.position.x > 15)
         {
             transform.Translate(moveLeftVector, Space.World);
         }
     }
 
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             PlayerTeleport();
         } 
         else if (!Input.GetButtonDown("Fire1"))
         {
             PlayerMove();
             PlayerJump();
             GravityEffect();
         }
     }
 }
Comment
Add comment · Show 4
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 xxmariofer · Jul 17, 2020 at 10:33 AM 0
Share

can you try this?

          if (transform.position.x < 14)
          {
              transform.Translate(moveRightVector, Space.World);
          } else if (transform.position.x > 14)
          {
              transform.Translate(moveLeftVector, Space.World);
          }
avatar image fladrifbirch xxmariofer · Jul 17, 2020 at 12:37 PM 0
Share

Still the same results for me :/ Thanks though!

avatar image xxmariofer fladrifbirch · Jul 17, 2020 at 01:42 PM 0
Share

well the code is fine then, is maybe lagging so so much? can you change this line

  if (Input.GetButtonDown("Fire1"))

to this one

  if (Input.Get$$anonymous$$ouseButtonDown(0))
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fladrifbirch · Jul 17, 2020 at 05:08 PM

If anyone else comes across this problem, I solved it by disabling the Character Controller when teleporting :)


 using System.Collections;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController controller;
 
     public Transform groundCheck;
     public float groundDistance;
 
     public float jumpHeight = 3f;
 
     public LayerMask groundMask;
 
     public float speed = 12f;
     public float gravity = -9.81f;
 
     Vector3 velocity;
     Vector3 moveRightVector = new Vector3(30f, 0f, 0f);
     Vector3 moveLeftVector = new Vector3(-30f, 0f, 0f);
     Vector3 playerStop = new Vector3(0f, 0f, 0f);
 
     bool isGrounded;
 
     private void PlayerGravity()
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         controller.Move(velocity * Time.deltaTime);
         velocity.y += gravity * Time.deltaTime;
 
         if (isGrounded && velocity.y < 0)
         {
             velocity.y = -2f;
         }
     }
     private void PlayerMove()
     {
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
 
         Vector3 move = transform.right * x + transform.forward * z;
 
         controller.Move(move * speed * Time.deltaTime);
     }
 
     private void PlayerJump()
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         controller.Move(velocity * Time.deltaTime);
 
         if (Input.GetButtonDown("Jump") && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
         }
     }
 
     private void PlayerTeleport()
     {
         if (transform.position.x < 14)
         {
             transform.Translate(moveRightVector, Space.World);
         } else if (transform.position.x > 14)
         {
             transform.Translate(moveLeftVector, Space.World);
         }
     }
 
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             controller.enabled = false;
             PlayerTeleport();
         } 
         else if (!Input.GetButtonDown("Fire1"))
         {
             controller.enabled = true;
             PlayerMove();
             PlayerJump();
             PlayerGravity();
         }
     }
 }
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

241 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Translating an Object to a Random Endpoint,Moving an Object to a random endpoint 0 Answers

GameObject doesn't move 1 Answer

"transform.Translate" doesn't works 2 Answers

What is the difference between translate and position 1 Answer

Perspective camera touch movement 2 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