Hey all!
My achillies heel is scripting.
thats why i need your help.
I have this script. it makes me use the WASD keys to move my tank around.
using UnityEngine;
using System.Collections;
public class HoverMotor : MonoBehaviour {
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody PlayerRigidbody;
void Awake ()
{
PlayerRigidbody = GetComponent ();
}
void Update ()
{
powerInput = Input.GetAxis (“Vertical”);
turnInput = Input.GetAxis (“Horizontal”);
}
void FixedUpdate()
{
Ray ray = new Ray (transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
PlayerRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
PlayerRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
PlayerRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
}
}
what i want it to do is to follow my mouse pointer for direction. and be able to use my A and D key to strafe left and right. Not turn left and right as im doing now.
how shall i do it?
the Tank has a fixed cannon on it, so it will shoot the direction the tank body is faced.
Start here:
If you see someone who needs to know about code tags, please link them to this post: Please use code tags when posting code. You can "tag" your code by typing around your code. There is no overt "Code" tag button that automatically tags a...
Reading time: 9 mins 🕑
Likes: 83 ❤
If you want the tank to move instead of rotate, you should add force and not torque.
I found MovePosition, MoveRotation was better suited to tank control
Forgot to say that its a hover tank.
It can hover at any direction. Doesnt need to turn.
It can strafe side to side.
using UnityEngine;
using System.Collections;
public class HoverMotor : MonoBehaviour {
public float speed = 90f;
public float turnSpeed = 5f;
public float hoverForce = 65f;
public float hoverHeight = 3.5f;
private float powerInput;
private float turnInput;
private Rigidbody PlayerRigidbody;
void Awake ()
{
PlayerRigidbody = GetComponent <Rigidbody>();
}
void Update ()
{
powerInput = Input.GetAxis ("Vertical");
turnInput = Input.GetAxis ("Horizontal");
}
void FixedUpdate()
{
Ray ray = new Ray (transform.position, -transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hoverHeight))
{
float proportionalHeight = (hoverHeight - hit.distance) / hoverHeight;
Vector3 appliedHoverForce = Vector3.up * proportionalHeight * hoverForce;
PlayerRigidbody.AddForce(appliedHoverForce, ForceMode.Acceleration);
}
PlayerRigidbody.AddRelativeForce(0f, 0f, powerInput * speed);
PlayerRigidbody.AddRelativeTorque(0f, turnInput * turnSpeed, 0f);
}
}