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 nighttyger · Feb 22, 2019 at 09:07 PM · addforceplayer movementcharacter controller

script failing to move player character

I'm trying to create a character controller that allows me to move a character in first person, with the right/left keys rotating 90 degrees, the back key rotating 180, and the forward key moving the character a set distance. Most of this is working, but when I try to actually move the character forward, I run into some issues. If I use the player.Move method, the character moves forward some variable amount between 0.5 and 0.9, with none of the involved variables seeming to affect the distance. I can't use this because the number is inconsistent - if it were the same each time, I could work with it. If I use AddForce, the character doesn't move at all. I would like to have either the AddForce version moving correctly, or the player.Move version moving consistently. Thank you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ControllerRedo : MonoBehaviour
 {
     public float moveDistance=50;
     public float playerX;
     public float playerZ;
     public float turn;
     public bool isMoving = false;
     public Vector3 movement;
     private CharacterController player;
     private Rigidbody rb;
 
     // Start is called before the first frame update
     void Start()
     {
         player = GetComponent<CharacterController>();
         rb = GetComponent<Rigidbody>();
         Debug.Log(transform.position);
     }
 
     public void Move()
     {
         if (Input.GetButtonDown("Horizontal"))
         {
             if (Input.GetKey(KeyCode.LeftArrow))
             {
                 turn = -90;
             }
 
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 turn = 90;
             }
         }
 
         if (Input.GetButtonDown("Vertical"))
         {
             if (Input.GetKey(KeyCode.DownArrow))
             {
                 turn = 180;
             }
 
 
             if (Input.GetKey(KeyCode.UpArrow))
             {
                 turn = 0;
                 RaycastHit hit;
                 if (Physics.Raycast(transform.position, transform.forward, out hit, 5) && hit.collider.tag != "Enemy")
                 {
                     return;
                 }
                 else
                 {
                     float zAxis = Input.GetAxis("Vertical") * moveDistance;
                     Vector3 movement = new Vector3(0, 0, zAxis);
                     movement = transform.rotation * movement;
                     //just comment out whichever of the two aren't needed to test
                     //rb.AddForce(transform.forward * moveDistance);    //doesn't move at all 
                     player.Move(movement);                    //moves inconsistently
                     Debug.Log(transform.position);
                 }
             }
         }
 
         transform.Rotate(0, turn, 0);
         turn = 0;
     }
 
     // Update is called once per frame
     void Update()
     {
         Move();
         //check collision for enemies
     }
 } 
Comment
Add comment · Show 3
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 RobAnthem · Feb 22, 2019 at 09:42 PM 0
Share

Horizontal and Vertical are axis, not buttons. You're requesting if a single input is down, when an axis consists of multiple inputs. What you need is something like...

 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical");
 if (h != 0)
 {
     //rotate object using h as the modifier, if h == 1, it's right, if h == -1 its left
     transform.Rotate(0, h * rotationSpeed * Time.deltaTime, 0);
 }
 if (v != 0)
 {
     //move object using v as the modifier, if v == 1 its forward if v == -1 its backward
     player.$$anonymous$$ove(transform.forward * moveDistance * v * Time.deltaTime);
 }
avatar image TreyH RobAnthem · Feb 22, 2019 at 09:51 PM 1
Share

For the 1/-1, do you mean GetAxisRaw?

avatar image RobAnthem TreyH · Feb 22, 2019 at 09:53 PM 1
Share

Yeah sorry, plus I assumed he's on a PC since he's calling out to $$anonymous$$eys and Axis' only result in being -1, 0, or 1, on a PC or whatever the $$anonymous$$/max values are set to in inspector.

0 Replies

· Add your reply
  • Sort: 

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

102 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

Related Questions

Character Controller "walking on air" off cliffs/slopes 1 Answer

Start Button 2 Answers

Move player via button press with including acceleration / decelertion? 0 Answers

Sliding after moving 0 Answers

Player keeps flying up 0 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