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 amelijac · Mar 10, 2020 at 09:50 PM · movementvector3sideways

why is my cube moving sideways in inconsistent distances?

I'm trying to make a cube move by 1 or -1 on the x axis when pressing "a" or "d" (and also making the cube move forwards, but that seems to be working). this is what i currently have.

 public Rigidbody rb;
     public float fforce = 2000f;
     public Vector3 side = new Vector3(1, 0, 0);
     public Transform pc;
     // Update is called once per frame
     void FixedUpdate()
     {
         rb.AddForce(0, 0, fforce * Time.deltaTime);
 
         if (Input.GetKeyDown(KeyCode.A))
         {
             transform.position = pc.position - side;
         }
         if (Input.GetKeyDown(KeyCode.D))
         {
             transform.position = pc.position + side;
         }

for example, when i press "d" sometimes the cube moves by 1 but sometimes by 2 or 3. I'm very new to unity so maybe this is a stupid question, but i really don't know whats the problem here and i'd appriciate 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 AlexMcLeod01 · Mar 10, 2020 at 10:49 PM 0
Share

I don't know without seeing what exactly you're trying to do, but I think you mean to either do: pc.position += side; and pc.position -= side;

OR

transform.position += side; and transform.position -= side;

$$anonymous$$y assumption is that transform and pc are pointing to different things, and you're likely wanting to use transform, if the script itself points to the cube.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pangamini · Mar 11, 2020 at 12:08 AM

My assumption is that it's because you are calling it from FixedUpdate, while Input's state gets updated in Update. Update is called once per frame, FixedUpdate is called a fixed amount of times per second. There can be multiple FixedUpdates per Update, there can be multiple Updates per FixedUpdate. It says in the documentation for GetKeyDown:

You need to call this function from the Update

So I'd try that. If you really have to apply the state change (the transform.position change) in the FixedUpdate, then read the Input in update, save it in some variable and read it from Fixedupdate

Comment
Add comment · Show 1 · 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 amelijac · Mar 11, 2020 at 08:10 AM 0
Share

thank you, that fixed it. i was using FixedUpdate because thats what Brackeys uses in his video, and i really didnt think twice about it. thanks

avatar image
0

Answer by krythix · Mar 11, 2020 at 02:01 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     //This script will control our player movement in our game
 
     public float moveSpeed;
     public float jumpForce;
 
     private Rigidbody rig;
 
     private void Awake()
     {
         //get the rigidbody component
         rig = GetComponent<Rigidbody>();
     }
 
     void Update()
     {
         Move();
 
         if(Input.GetButtonDown("Jump"))
         {
             TryJump();
         }
     }
 
     void Move()
     {
         //getting our player control inputs
         float xInput = Input.GetAxis("Horizontal"); //will return an X value of -1 to 1. If no button is pressed then value will be 0
         float zInput = Input.GetAxis("Vertical");//will return a Y value of -1 to 1. If no button is pressed then value will be 0
 
         Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
         dir.y = rig.velocity.y; //setting y to whatever existing velocity is
 
         rig.velocity = dir; //applying our Vector3 to our rigidbody
         //note: the 'velocity' is a Rigid3 itself as well ^
 
         Vector3 facingDir = new Vector3(xInput, 0, zInput); // 'magnitude' is this vector's length
 
         if(facingDir.magnitude > 0) //if magnitude is NOT zero, then we change the facing direction forward with this code
         {
             transform.forward = facingDir;
         }
     }
 
     void TryJump()
     {
 
         Ray ray = new Ray(transform.position, Vector3.down); //we are shooting the raycast in a downward position to check if we are on the floor
 
         if(Physics.Raycast(ray, 0.7f)) // first value 'ray' is the direction we're shooting in, 0.7f is how faf we are shooting the raycast
         {
             rig.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 krythix · Mar 11, 2020 at 02:02 AM 0
Share

It's still not working, character cube is floating above the ground just a little

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

204 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

Related Questions

Stopping Vector3.Lerp 3 Answers

Camera wil not move between given points, stays at first given point 1 Answer

Choppy y-axis crane movement when grabbing food, not dropping food on target 0 Answers

Plotting particle movement Time/keyframe issue 0 Answers

'Moving' a fixed Distance to the left/right 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