Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity 3d camera movement script

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

public class thirdpersonmovement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;

    public float speed = 6f;

    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);

        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: stack in c# 
Csharp :: notification platform not available c# 
Csharp :: c# 2 timespan return yesterday 
Csharp :: c# stringwriter encoding iso-8859-1 example 
Csharp :: c# sprintf equivalent 
Csharp :: Jeng InitDb 
Csharp :: lamda expression multiple and 
Csharp :: RGB Arduino uno r3 
Csharp :: get centerpoint of points transforms 
Csharp :: permutation and combination program in c# 
Csharp :: random.choice c# 
Csharp :: firepower 4125 License update 
Csharp :: c# check if username and password is true 
Csharp :: resize image and add watermark c# 
Csharp :: two question marks c# 
Csharp :: c# extend array 
Csharp :: dotnet core vue in subdirectory 
Csharp :: c# ulong 
Csharp :: how to run a console app in another app c# 
Csharp :: How to read key from web config in JavaScript 
Csharp :: csvhelper driver c# nuget 
Csharp :: trigger checkbox combobox wpf 
Csharp :: get access to all controls with a specific tag in C# 
Csharp :: c# read only file used by other app 
Csharp :: why process not found in c# 
Csharp :: Avoid auto-filling persian time picker 
Csharp :: c# .net calculate md5 
Csharp :: multiple lines in string c# parameterized 
Csharp :: c# lernen kostenlos 
Csharp :: getcomponent rigidbody2d 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =