does anybody know how to make a simple movement script in c#? (for the player)
my attempts have been fails
does anybody know how to make a simple movement script in c#? (for the player)
my attempts have been fails
What do you mean? like a character controller movement?
yeah
sorry it’s kinda taking long hold on
why don’t you use the standard assets javascript charactermotor?
Ok this is what i have, just a java script version on the FPSWalker script from Unity2.6
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float speed = 4.0F;
public float jumpSpeed = 4.0F;
public float gravity = 20.0F;
private CharacterController controller;
private Vector3 moveDirection;
// Use this for initialization
void Start ()
{
controller = GetComponent();
if(!controller)
controller = gameObject.AddComponent();
}
// Update is called once per frame
void Update ()
{
if(controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis(“Horizontal”),0,Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButton(“Jump”))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
I hope that helps ![]()
i try to use c# for consistency and I have 2.64
bump
you can translate the j script to C#
Sorry, but you can’t expect anybody to provide you an out-of-the-box movement script in C# that would perfectly fit your game without any change from you.
There are too many variables to consider.
What Nomad said.
If you can’t figure out how to move something using Input.GetAxis and Transform.Translate:
http://unity3d.com/support/documentation/ScriptReference/Input.GetAxis.html
http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html
You need to start with some programming tutorials or partner with a programmer.
Here is one, you can use any of the code as you fit. You just have to add necessary buttons to your input control.
It jumps with a delay, walks, runs, falls and you can apply damage, also pushes rigidbodies around, doesnt climb high slopes, if try to jump and climb high slopes, it just pushes you back so you fall…etc.
using UnityEngine;
using System.Collections;
//Input class
internal static class Inp {
internal static float px, pz, ax, ay;
internal static bool run, jump, crouch;
}
[AddComponentMenu ("Character/Player")]
[RequireComponent (typeof (CharacterController))]
public class Player : MonoBehaviour {
//Components
internal GameObject go;
internal Transform body, cam;
internal CharacterController cc;
internal Rigidbody rbd;
//Publics
//Rotation
public float mouseSensitivity = 10.0F;
//Motion
public float walkSpeed = 6.0F;
public float runSpeed = 10.0F;
public float jumpHeight = 2.0F;
public int jumpDelay = 5;
public float gravity = 20.0F;
public float maxFallDistance = 6.0F;
public float pushPower = 2.0F;
public LayerMask pushLayers = -1;
//Privates
//Rotation
private Quaternion qBody, qCam;
//Motion
private Vector3 moveDir;
private int jumpTimer;
private float fallBegin;
internal bool jumping, falling;
private Vector3 slopeNormal;
//Construction
void Awake () {
//Components
go = gameObject;
body = go.transform;
cam = Camera.main.transform;
cc = go.GetComponent<CharacterController> ();
rbd = go.GetComponent<Rigidbody> ();
//Rotation
if (rbd) rbd.freezeRotation = true;
qBody = body.localRotation;
qCam = cam.localRotation;
}
void Start () {
//Input Reset
Inp.ax = Inp.ay = 0.0F;
//Motion Reset
moveDir = Vector3.zero;
jumpTimer = jumpDelay;
fallBegin = 0.0F;
falling = false;
}
void Update () {
if (Core.App.paused)
return;
//Keyboard Input
Inp.px = Input.GetAxis ("Strafe");
Inp.pz = Input.GetAxis ("Walk");
Inp.run = Input.GetButton ("Run");
Inp.jump = Input.GetButton ("Jump");
Inp.crouch = Input.GetButton ("Crouch");
//Mouse Input
Inp.ax += Input.GetAxis("Mouse X") * mouseSensitivity;
Inp.ax = Core.Math.AngleWrap(Inp.ax, -360, 360);
Inp.ay -= Input.GetAxis("Mouse Y") * mouseSensitivity;
Inp.ay = Core.Math.AngleWrap(Inp.ay, -60, 60);
//Handle Rotation
body.localRotation = qBody * Quaternion.AngleAxis (Inp.ax, Vector3.up);
cam.localRotation = qCam * Quaternion.AngleAxis (Inp.ay, Vector3.right);
}
//Update physics
void FixedUpdate () {
if (Core.App.paused)
return;
if (cc.isGrounded) {
jumping = false;
//Walking
moveDir = new Vector3 (Inp.px, 0.0F, Inp.pz);
moveDir = body.TransformDirection (moveDir);
if (!Inp.run) moveDir *= walkSpeed;
else moveDir *= runSpeed;
//Jumping
if (!Inp.jump)
jumpTimer++;
else if (jumpTimer >= jumpDelay) {
jumping = true;
if (Vector3.Angle (slopeNormal, Vector3.up) >= cc.slopeLimit)
moveDir = slopeNormal.normalized * walkSpeed;
if (!Inp.crouch)
moveDir.y = Mathf.Sqrt(2.0F * jumpHeight * gravity);
else
moveDir.y = Mathf.Sqrt(2.0F * (jumpHeight * 2.0F) * gravity);
jumpTimer = 0;
}
//Fall Damage Calculate
if (falling) {
falling = false;
if (body.position.y < fallBegin - maxFallDistance)
FallDamage (fallBegin - body.position.y);
}
}
//Falling
else {
if (!falling) {
falling = true;
fallBegin = body.position.y;
}
}
//Gravity
moveDir.y -= gravity * Time.deltaTime;
//Move cc
CollisionFlags flags = cc.Move (moveDir * Time.deltaTime);
if (flags == CollisionFlags.Below)
return;
}
void OnControllerColliderHit (ControllerColliderHit hit) {
slopeNormal = hit.normal;
Rigidbody hitBody = hit.collider.attachedRigidbody;
if (hitBody == null || hitBody.isKinematic)
return;
LayerMask hitBodyLayerMask = 1 << hitBody.gameObject.layer;
if ((hitBodyLayerMask pushLayers.value) == 0)
return;
if (hit.moveDirection.y < -0.3F)
return;
Vector3 pushDir = new Vector3 (hit.moveDirection.x, 0.0F, hit.moveDirection.z);
hitBody.velocity = pushDir * pushPower * GetHorizontalSpeed();
}
//Get walking speed
public float GetHorizontalSpeed () {
Vector3 spd = cc.velocity;
spd.y = 0.0F;
return spd.magnitude;
}
//Get jumping speed
public float GetVerticalSpeed () {
return cc.velocity.y;
}
//Get total speed
public float GetOverallSpeed () {
return cc.velocity.magnitude;
}
//Apply falldamage
private void FallDamage (float fallDistance) {
Debug.Log ("Ouch! Fell " + fallDistance + " units!");
}
}
it says that core does not exist in the current context, any ideas?
like n0mad said, you need to script things youself, i thought you wanted a basic movement script in C#, scripting is VITAL in learning Unity3D. I hope the scripts help, but you really need to learn using them. I hope you game turns out great.
Yes, because they make use of something you’re not making use of or something you don’t need. The script he gave you is an example. Learn from it.
thanks guys, disecting the example script.
Remove the below lines from Update and Fixedupdate
if (Core.App.paused)
return;
You dont need that part.