• 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 MaloMn · Dec 02, 2015 at 08:30 PM · movementplayer

Huge problem with moving my player

Hello!

I would ask you if you'd know how to find the problem I have. I made my game in Unity, with assets that I've made within Blender. The problem is I can't move the player. I tried everything, went on many forums, asked my brother who work as developers and he wasn't able to find my problem, so I hope you will!

So, I have a mesh, and I added to it a rigidbody component and a boxcollider, and I then added the script founded there : https://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player?playlist=17141 I've just changed the vector3 to a vector2 because I didn't need the Z axis. I tried with the original script and with the one modified and both didn't work. I went to the input settings, put "left" and "right" to use the arrow and nothing worked. Could you tell me where the problem is? If you need to, I can send you screenshots of my scene! Thanks in advance :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Dec 03, 2015 at 05:10 PM

There are two things you need to clarify here. 1; Is the character static state (no bones/rigged?) or is it a humanoid model with boned/rigged?

1; If it is a static state object with no moving parts and you added the script to the player model itself it should work. Also make sure its tagged "Player" and not just named Player.

2: Rigidbody in Unity is not the same as Rigged Body(bones). Its for your setting with gravity, position, etc.

The other is what version of Unity are you using? You have to also be certain what sort of movement you are looking for because the script will also determine of you are just moving around or if you will pass through things.

Without seeing the script or knowing what sort of model you are trying to use makes helping you properly rather difficult.

Here is an example of some basic script concepts that don't call for animations of a character with bones/rigged body, but you still need to have rigid body and some sort of collider on it.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
 
     public float moveSpeed;
 
     // Use this for initialization
     void Start()
     {
         moveSpeed = 50.0f;
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
 
     }
 }

Notice there are no rotations in this script. Its just for moving forward and backward or side to side. However, there is no force detection. In void start you have the speed here set at 50 which is really fast, so you would need to figure out what works best and can set it in the script or in the editor.

Now compare the one from rollerball

 public float speed;
 
     private Rigidbody rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rb.AddForce (movement * speed);
     }
 }

and the one from SpaceShooter

 void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rigidbody.velocity = movement * speed;
         rigidbody.position = new Vector3 

and the one without the animations part from Survival Shooter

void FixedUpdate () { // Store the input axes. float h = Input.GetAxisRaw ("Horizontal"); float v = Input.GetAxisRaw ("Vertical");

Notice the ones from Unity all say the same thing more or less, just some parts abbreviated.

Then you have more or less this version here from this link(https://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate)

 {
     public float moveSpeed = 10f;
     public float turnSpeed = 50f;
     
     
     void Update ()
     {

//Note this set is just for moving foirward/backward//

         if(Input.GetKey(KeyCode.UpArrow))
             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
          if(Input.GetKey(KeyCode.DownArrow))
             transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);

//Note this is for the rotations left to right//

         if(Input.GetKey(KeyCode.LeftArrow))
             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
         if(Input.GetKey(KeyCode.RightArrow))
             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
     }
 }

Now then there is this version that locks you to the ground and allows the interaction needed,

//Feed moveDirection with input.

         transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);

         Vector3 forward = transform.TransformDirection(Vector3.forward);

         float curSpeed = speed * Input.GetAxis("Vertical");

         controller.SimpleMove(forward * curSpeed);

I hope this was helpful, and note I personally cannot get animated characters working correctly on my end, and this is all for static state character objects, like a craft of some sort.

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 $$anonymous$$ · Dec 03, 2015 at 05:18 PM 0
Share

PS. You will still need to attack a rigid body and make sure you have a collider on it, Using the Character Controller collider tends to work best and its a good idea to call it from the script in the current version of Unity 5 as

  CharacterController controller = GetComponent<CharacterController>();

Notice its called redundantly 3 times in this line. A lot of this is explained in these options section under "Scripting."

https://unity3d.com/learn/tutorials/topics/scripting

Sometimes they can be vague, but that's one reason you can go through all the tutorials, and check the specific areas of code to see what the instructors are using here, and what others have been doing on their own and willing to share.

Sorry this was a hell of a lot more than most will point out, but frankly most also do not show comparisons either to help us newbs grasp the basics.

avatar image
0

Answer by MaloMn · Dec 05, 2015 at 04:06 PM

Hello!

First, I wanna say thank you because you told me everything that I needed for my game! :) Then, I used the first script and I've modify it a little bit with the help of my brother and he added what was needed more specifically for my game. Here's the script: using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

 private float moveSpeed;
 private const float JUMP_UNIT = 1.5f;
 private const float MOVE_UNIT = 0.5f;
 private bool isJumping = false;

 private Vector3 nextPosition = Vector3.zero;

 // Use this for initialization
 void Start()
 {
     moveSpeed = 50.0f;
     Physics.gravity = new Vector3(0, -75.0F, 0);
 }

 public void Update4()
 {
     if (Time.time < 2)
         return;

     if (nextPosition.Equals(Vector3.zero)) // Initialize next position
         nextPosition = transform.position;

     if (nextPosition != transform.position) // Wait till player has reached next position

Again, thank you so much for your time and your experience! :) return;

     if (Input.GetKey(KeyCode.UpArrow))
         Jump();
     if (Input.GetKey(KeyCode.DownArrow))
         Bend();
 }

 private void Jump()
 {
     transform.Translate(0.5f, 0f, 0f);
     nextPosition += new Vector3(0.5f, 0f, 0f);
 }

 private void Bend()
 {

 }

 // Update is called once per frame
 void Update()
 {
     float x = 0;
     float y = 0;

     if (Input.GetKey(KeyCode.UpArrow))
         x = -JUMP_UNIT;
     if (Input.GetKey(KeyCode.DownArrow))
         x = 0;
     if (Input.GetKey(KeyCode.LeftArrow))
         y = MOVE_UNIT;
     if (Input.GetKey(KeyCode.RightArrow))
         y = -MOVE_UNIT;
     
     if (x < 0) // Player tries to jump
     {
         if (isJumping)
             x = 0; // Avoid double-jumps
         isJumping = !isJumping;
     }


     if (Input.GetKey(KeyCode.DownArrow))
     {
        // transform.localScale -= new Vector3(0.001f, 0f, 0f);
     } else
     {
         //transform.localScale += new Vector3(0.001f, 0f, 0f); ;
     }

     transform.Translate(
         moveSpeed * x * Time.deltaTime,
         moveSpeed * y * Time.deltaTime,
         0f
     );
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Help] Player stops moving when hitting wall diagonally 0 Answers

Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers

How to move the player only 1 tile per buttonPress? 2 Answers

Navmesh based player movement 1 Answer

How do I add animation to my script? 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