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 BadwolfX · Jan 13, 2018 at 01:03 PM · movement scriptforwardconstant

Change to constantly move forward

Hello, I have this piece of code and after coming back to my scripting days I have wanted to change it, I would really for the character to constantly move forward. I'm really sorry, could anyone help me with this? Code below :) This movement script is for moving around a planet (a sphere with its own gravity) using UnityEngine; using System.Collections;

     [RequireComponent (typeof (GravityBody))]
     public class FirstPersonController : MonoBehaviour {
         
         // public vars
         public float mouseSensitivityX = 1;
         public float mouseSensitivityY = 1;
         public float walkSpeed = 6;
         public float jumpForce = 220;
         public LayerMask groundedMask;
         
         // System vars
         bool grounded;
         Vector3 moveAmount;
         Vector3 smoothMoveVelocity;
         float verticalLookRotation;
         Transform cameraTransform;
         Rigidbody rigidbody;
         
         
         void Awake() {
             Cursor.lockState = CursorLockMode.Locked;
             Cursor.visible = false;
             cameraTransform = Camera.main.transform;
             rigidbody = GetComponent<Rigidbody> ();
         }
         
         void Update() {
             
             // Look rotation:
             transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivityX);
             verticalLookRotation += Input.GetAxis("Mouse Y") * mouseSensitivityY;
             verticalLookRotation = Mathf.Clamp(verticalLookRotation,-60,60);
             cameraTransform.localEulerAngles = Vector3.left * verticalLookRotation;
             
             // Calculate movement:
             float inputX = Input.GetAxisRaw("Horizontal");
             float inputY = Input.GetAxisRaw("Vertical");
             
             Vector3 moveDir = new Vector3(inputX,0, inputY).normalized;
             Vector3 targetMoveAmount = moveDir * walkSpeed;
             moveAmount = Vector3.SmoothDamp(moveAmount,targetMoveAmount,ref smoothMoveVelocity,.15f);
             
             // Jump
             if (Input.GetButtonDown("Jump")) {
                 if (grounded) {
                     rigidbody.AddForce(transform.up * jumpForce);
                 }
             }
             
             // Grounded check
             Ray ray = new Ray(transform.position, -transform.up);
             RaycastHit hit;
             
             if (Physics.Raycast(ray, out hit, 1 + .1f, groundedMask)) {
                 grounded = true;
             }
             else {
                 grounded = false;
             }
             
         }
         
         void FixedUpdate() {
             // Apply movement to rigidbody
             Vector3 localMove = transform.TransformDirection(moveAmount) * Time.fixedDeltaTime;
             rigidbody.MovePosition(rigidbody.position + localMove);
         }
     }
     
 

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 Diukrone · Jan 13, 2018 at 11:46 PM 0
Share

GetComponent().velocity = transform.forward * moveAmount;

i would use it ins$$anonymous$$d of :

moveAmount = Vector3.SmoothDamp(moveAmount,target$$anonymous$$oveAmount,ref smooth$$anonymous$$oveVelocity,.15f);

$$anonymous$$aybe it can help!

1 Reply

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

Answer by Diukrone · Jan 13, 2018 at 01:26 PM

You should use AddForce.

Comment
Add comment · Show 13 · 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 BadwolfX · Jan 13, 2018 at 09:10 PM 0
Share

sorry for the late reply, but where?

avatar image Diukrone · Jan 13, 2018 at 11:56 PM 1
Share

@BadwolfX sorry mate, i was wrong. You need to call the Rigidbody component from where you attached your script and to change it´s velocity by using the Velocity class from Rigidbody!.

In the way you are doing, i know this way very well. $$anonymous$$ovePosition will teleport your object, not really to move it. Avoid to use SmoothDamp to velocity and use Lerp to keep a constant velocity data.

I would use SmoothDamp to Axis Input values and Lerp to constant velocity. Ah yeah, all your rigidbody calculations (Vector3 and Quaternion) need to be in Fixed Updated. On Update just put things that need to start and on LateUpdate only let the less used commands in game, as: loading animations, trigger events, pressing a key, changing câmera positions, etc.

You need to rewrite your script and to put functions and class in the right parts of the code. You are almost there.

On FixedUpdate, you can try all your SmoothDamp values on this code, on FixedUpdate:

 myPrefab.AddForce(Vector3.up * gravityStability);
     myPrefab.rotation = Quaternion.Euler(new Vector3(smoothX, lerpY + smoothY, smoothZ));

Remember that you can substitue the smooth values by random numbers just to test the code. There where i Lerp you put your velocity calculation value using Lerp and it will Works also with smoothed Y axis inputs or whatever.

avatar image Diukrone Diukrone · Jan 14, 2018 at 12:10 AM 0
Share

Quaternion.Euler(new Vector3(0, 0, 0);

You use NEW VECTOR3 just to create smooth while moving it.

If do not to have the new Vector3, the values will change instantly the prefab. Eveything that will change the transform position need to be smoothed with new Vector3.

avatar image BadwolfX Diukrone · Jan 15, 2018 at 06:06 PM 0
Share

Thank you mate worked great :)

Show more comments

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

75 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

Related Questions

Character rotates left and right instead of going forward. 1 Answer

360 Degree Snake 0 Answers

Dodge movement - restrict movement of an object left and right 0 Answers

Question about moving an object technique 3 Answers

Moving tram along rail. 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