Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Help with movement script

Discussion in 'Scripting' started by amarsaranovic, Jul 16, 2018.

  1. amarsaranovic

    amarsaranovic

    Joined:
    Jul 16, 2018
    Posts:
    4
    Code (CSharp):
    1.    
    2. using UnityEngine;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.    
    7.     public Rigidbody rb; // This is a reference to the Rigidbody component called "rb"
    8.     public float forwardForce = 800f;
    9.     public float moveSpeed = 10f;
    10.  
    11.      
    12.     // We marked this as "Fixed"Update because we
    13.     // are using it to mess with the physics.
    14.     void FixedUpdate ()
    15.     {
    16.        
    17.         rb.AddForce(0f, 0f, forwardForce * Time.deltaTime);  // Add a forward force
    18.  
    19.         transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f, ForceMode.VelocityChange);
    20.        
    21.     }
    22.  
    23. }
    24.  
    Assets/PlayerMovement.cs(18,105): error CS1503: Argument `#4' cannot convert `UnityEngine.ForceMode' expression to type `UnityEngine.Space'
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    amarsaranovic likes this.
  3. amarsaranovic

    amarsaranovic

    Joined:
    Jul 16, 2018
    Posts:
    4
    Thank you for your reply! I am currently using the whole of line 19 to move a cube with arrow keys left and right. Is there any way to implement ForceMode.VelocityChange to that movement?
     
  4. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Well, you don't actually need ForceMode.VelocityChange for that to work. Why do you want to use ForceMode.VelocityChange? That can only be used with Rigidbody functions, transform.Translate is not a rigidbody function. You are able pass ForceMode.VelocityChange into line 17, because AddForce is a rigidbody function.
     
  5. amarsaranovic

    amarsaranovic

    Joined:
    Jul 16, 2018
    Posts:
    4
    Well the controls feel very very slow and I saw a tutorial on youtube. He used ForceMode.VelocityChange and it worked very nice. The problem is he didnt use the same method as me. He used:
    if (Input.GetKey(d))
    {
    rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
    Is this a better method than mine to move a cube left and right.
    I was originaly following that tutorial but it he said that method wasnt the best so I made the whole transform.translate thing.
    (Btw. sorry for all of the stupid questions I am fairly new to C#, and the poor gramar(Im not a native english speaker)).
    What is your opinion on what I should do?
    Thanks in advance!
     
  6. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    It's alright, we all have to start somewhere ;)

    If the controls feel very slow, you could increase
    moveSpeed
    to a larger number to get it to respond quicker.
    Personally, I don't think you should use transform.Translate with rigidbody's, as it's not a very stable approach. You should choose one or the other.

    You could use ONLY rigidbody functions like this:
    Code (CSharp):
    1. Vector3 force = new Vector3 (moveSpeed * Input.GetAxis("Horizontal"), 0.0f, forwardForce);
    2. rb.AddForce(force, ForceMode.VelocityChange);
    Or use you could use transform.Translate only like this:
    Code (CSharp):
    1. transform.Translate (moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0.0f, forwardForce * Time.deltaTime);
    You can choose which method you prefer.
    Also when using Rigidbody.AddForce, you should not multiply the forces by Time.deltaTime, but when using transform.Translate, you should.
     
    Last edited: Jul 16, 2018
    amarsaranovic likes this.
  7. amarsaranovic

    amarsaranovic

    Joined:
    Jul 16, 2018
    Posts:
    4
    Thank you very much!!!
     
  8. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Your welcome!