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 Darmouth · Nov 08, 2013 at 05:19 AM · c#movementmovement scriptspeed

Movement speed doesn't change with variable C#

Hey, I'm making a 2.5D puzzle platformer, and I have my character movement code working, with the exception of a variable speed. This is my code in C#:

using UnityEngine; using System.Collections;

public class CharacterMovement : MonoBehaviour {

 //Variables
 public GameObject player;
 public float turnspeed=180f;
 public float speed = 6.0F;
 public float jumpSpeed = 8.0F; 
 public float gravity = 20.0F;
 private Vector3 moveDirection, moveVector = Vector3.zero;
 
 
 void Update() {
     CharacterController controller = GetComponent<CharacterController>();
     // is the controller on the ground?
     if (controller.isGrounded) {
         //Feed moveDirection with input.
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         moveVector = transform.TransformDirection(moveDirection);
         //Multiply it by speed.
         moveDirection *= speed;
         if (moveVector != Vector3.zero){
             if(Vector3.Angle (transform.forward, moveVector) > 179){
                 moveVector = transform.TransformDirection (new Vector3(.01f, 0, -1));
             }
             player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, Quaternion.LookRotation (moveVector), turnspeed * Time.deltaTime);
         }
         //Jumping
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
     //Applying gravity to the controller
     moveDirection.y -= gravity * Time.deltaTime;
     //Making the character move
     controller.Move (moveDirection * Time.deltaTime);

} }

The only difference in movement speed is whether or not the speed variable is =< 1. at any speed >1, it moves the same amount, regardless of if it's 10 or 10,000. What am I doing wrong?

EDIT: There's also a latency issue, where the character continues moving for a moment after releasing the movement button. Any help on that too would be greatly appreciated.

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 Huacanacha · Nov 08, 2013 at 05:22 AM 0
Share

Half of your code didn't format... difficult to read the meaning is.

avatar image Huacanacha · Nov 08, 2013 at 05:54 AM 0
Share

I can't see anything obvious... just start throwing Debug.Logs around all the places you use speed and update moveDirection. $$anonymous$$ake sure the values are what you expect them to be at each step.

I assume you mean lateral (x,y) movement is limited, not the jumping?

avatar image AR_Rizvi · Nov 08, 2013 at 06:10 AM 0
Share

Why u r $$anonymous$$ultiplyng

moveDirection *= speed; here and then

controller.$$anonymous$$ove (moveDirection * Time.deltaTime); here again

as i am thinking its making the speed multiply at first and then reducing it again by multiplying it with Time.deltaTime

avatar image Darmouth · Nov 09, 2013 at 04:55 AM 0
Share

@Huacanacha, yeah, the (x,z) movement is limited, not the jumping. I'll try throwing some Debug.Logs.

@AR_Rizvi, I know that it's reducing it, but even so, if I set the speed to 10000, I should still see a difference in speed than if I set speed to 10, and I don't. Even when I remove the "* Time.deltaTime," it doesn't make a difference.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Starwalker · Nov 08, 2013 at 08:51 PM

 moveDirection *= speed;

You apply speed here, ie 6. (above)

  controller.Move (moveDirection * Time.deltaTime);

You remove your speed from 6 to 0.25 or something less (in .Move), Time.deltaTime is the time in secs it took the last frame to complete, and its a small value, so your multiplying 6 * 0.1 (example) = 0.6, and deltaTime will be usually in that range (unless new stuff gets added), so you need to add the result after you multiply it.

Like so: moveDirection += moveDirection * Time.deltaTime;

You have copy-pasted the example code from Unityscripts, controller, don't modify something you don't fully understand event wise. It will break.

 if (moveVector != Vector3.zero){
          if(Vector3.Angle (transform.forward, moveVector) > 179){
           moveVector = transform.TransformDirection (new Vector3(.01f, 0, -1));
          }

This if block will nullify your moveVector's X value to 0.01, which I have no clue, why you have put it in the same code block. Thats your next problem.

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 Darmouth · Nov 09, 2013 at 04:55 AM 0
Share

As for the first part, that shouldn't matter if I set speed to something crazy like 10,000, but it still moves the same speed as if I set it to 10. As for the second part, I freely admit that I don't fully understand how that code works, however I understand it enough that the modifications I've made work for what I need it to do. The variable moveVector has nothing to do with the actual movement of the controller, so it doesn't really matter what I put there anyway, does it?

EDIT: after playing around with that bit of code, and removing it, my model still does what I want it to, so Idk why it was there in the first place.

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

18 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Movement of increased certain speed type not registering. 0 Answers

Bullet not moving from script 3 Answers

Can't change the speed of a character. 2 Answers

Smooth Movement with set distance 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