Answers for "3d movement script unity"

C#
3

unity 3d movement script 2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class PlayerController : MonoBehaviour
{
    CharacterController characterController;
    public float MovementSpeed =1;
    public float Gravity = 9.8f;
    private float velocity = 0;
 
    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }
 
    void Update()
    {
        // player movement - forward, backward, left, right
        float horizontal = Input.GetAxis("Horizontal") * MovementSpeed;
        float vertical = Input.GetAxis("Vertical") * MovementSpeed;
        characterController.Move((Vector3.right * horizontal + Vector3.forward * vertical) * Time.deltaTime);
 
        // Gravity
        if(characterController.isGrounded)
        {
            velocity = 0;
        }
        else
        {
            velocity -= Gravity * Time.deltaTime;
            characterController.Move(new Vector3(0, velocity, 0));
        }
    }
}
Posted by: Guest on November-06-2020
16

movement script c#

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class ExampleClass : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;

    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);
    }
}
Posted by: Guest on June-05-2020
12

movement script unity

//make sure to add a CharacterController to the thing that you want to move
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    CharacterController characterController;

    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float speed = 9.0f;

    private Vector3 moveDirection = Vector3.zero;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));

        if (characterController.isGrounded)
        {

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
        moveDirection.y -= gravity * Time.deltaTime;
        characterController.Move(moveDirection * Time.deltaTime);
    }
}
Posted by: Guest on October-26-2020
9

unity move character

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float speed = 5.0f;

    private void Update()
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");
        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));
    }
}
Posted by: Guest on June-30-2020
2

how to make 3d player movement in unity

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

//Apply this script to what you want to move

public int MovementSpeed;

void Update(){


if(Input.GetKey("w"))
{
	transform.position += Vector3.forward * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("d"))
{
	transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("a"))
{
	transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("s"))
{
	transform.position += Vector3.back * MovementSpeed * Time.deltaTime;
}
Posted by: Guest on August-25-2021
-1

3d movement script unity

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

public class PlayerMovement : MonoBehaviour
{
    private float MoveX, MoveZ;
    //sd is movement speed
    public float sd;
    public float JumpForce;
    public bool IsGrounded;
    public Rigidbody rb;
    //ms is mouse speed
    public int ms;
    public Transform cam;
    private float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        float mouseX = Input.GetAxis("Mouse X") * ms * 10 * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * ms * 10 * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        cam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        this.gameObject.transform.Rotate(Vector3.up * mouseX);
        
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        MoveX = Input.GetAxis("Horizontal");
        MoveZ = Input.GetAxis("Vertical");
        rb.MovePosition(transform.position + (transform.forward * MoveZ * sd/2) + (transform.right * MoveX * sd/2));
        if(Input.GetKey(KeyCode.LeftControl))
        {
            if(Input.GetKey(KeyCode.W))
            {
                rb.MovePosition(transform.position + (transform.forward * MoveZ * sd) + (transform.right * MoveX * sd));
            }
        }
        if(Input.GetKeyDown(KeyCode.Space) && IsGrounded)
        {
            rb.velocity = new Vector3(0f,JumpForce,0f);
        }
    }
    void OnCollisionStay()
    {
        IsGrounded = true;
    }
    void OnCollisionExit()
    {
        IsGrounded = false;
    }
}
Posted by: Guest on August-03-2021

C# Answers by Framework

Browse Popular Code Answers by Language