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 faisalshakeelkhan · Aug 01, 2019 at 06:14 PM · movement3d

Player moving automatically in 3D

I have just started learning unity. I am working on a 3D game. I have created a player gameobject from a capsule. I have added a Player.cs and LookX.cs script to the player gameobject, these control the movement from keyboard and horizontal mouse movement.

I have created an empty gameobject called LookY and added it inside player gameobject and i added the Main camera inside this LookY Gameobject.

I have attached the LookY.cs script to the LookY gameobject. When game starts, i move the mouse perfectly fine and walk around fine but i continuously keep moving in one direction slowly even i haven't pressed any buttons,

I suspect the player.cs to have the fault but no errors show up.

How can i stop it from moving on its own. Also the player was moving in reverse so i set the y axis rotation of main camera to 180.

Since i can attach only 1 file, i am writing all scripts in 1 file attached below.

link text

looky.txt (3.0 kB)
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 Magso · Aug 01, 2019 at 06:25 PM 0
Share

You can post the input statements of Player.cs here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by faisalshakeelkhan · Aug 02, 2019 at 09:26 AM

This is the player.cs code :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : MonoBehaviour { //used to control player movement private CharacterController _controller; //player movement speed [SerializeField] private float _speed = 3.5f;

 private float _gravity = 9.81f;

 

 void Start()
 {
     //getting access to player
     _controller = GetComponent<CharacterController>();


 }

 // Update is called once per frame
 void Update()
 {
     CalculateMovement();
 }

 void CalculateMovement()
 {
     
     
     
        float horizontalmovement = Input.GetAxis("Horizontal");
     
        float verticalmovement = Input.GetAxis("Vertical");
     
     
     
     //Y is zero as it controls our jumping movement and Z controls forward and backward
     Vector3 direction = new Vector3(horizontalmovement, 0, verticalmovement);
     //player speed and direction
     Vector3 velocity = direction * _speed;

     velocity.y -= _gravity;
     //this transforms local space to world space
     //local space values  to be converted to world space so player can correctly move w.r.t the camera movement
     velocity = transform.transform.TransformDirection(velocity);
     //applying gravity on y axis of player.

     _controller.Move(velocity * Time.deltaTime);
 }

}

Comment
Add comment · Show 2 · 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 Magso · Aug 02, 2019 at 02:14 PM 0
Share

I can't see why it would be constantly moving here :/ Try checking the Input vars using this

 Debug.Log(direction);
avatar image faisalshakeelkhan Magso · Aug 02, 2019 at 03:24 PM 0
Share

Thanks for taking the time to reply to this.

I removed all scripts and game objects and started doing it from scratch again, now it is working fine,

The reason I assume is that when I added the LookY gameobject inside Player, I didn't set it to 0,0,0 relative to the player.

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

188 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

Related Questions

Best tutorial for movement programing? 1 Answer

3d Space Movement 1 Answer

Locking movement to one axis only (3D game) 1 Answer

Object's vectors are not moving with the object, what did i do wrong? 1 Answer

How to add platform motion to player controller without parenting? - Mostly working code 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