OK This is a very basic & simple car driving & steering script I don’t remember where I got it but it works great for what it is & I like how it handles when used to swim on top of water…I call it SwimControl
var speed : float = 10.0;
var rotationSpeed : float = 20.0;
function Update () {
// Get the horizontal and vertical axis.
// By default they are mapped to the arrow keys.
// The value is in the range -1 to 1
var translation : float = Input.GetAxis (“Vertical”) * speed;
var rotation : float = Input.GetAxis (“Horizontal”) * rotationSpeed;
// Make it move 10 meters per second instead of 10 meters per frame…
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
// Move translation along the object’s z-axis
transform.Translate (-0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
}
This is the very simple basic buoyancy script im using to float the player…I call it SwimFloat
public var force: float;
var rb: Rigidbody;
public var water: GameObject;
private var Player : GameObject;
function Update () {
Player = gameObject;
rb = GameObject.FindWithTag(“HitBox”).GetComponent(Rigidbody);
if(transform.position.y < water.transform.position.y)
rb.AddForce(transform.up * force * 10);
if(transform.position.y > water.transform.position.y)
rb.AddForce(-transform.up * force * 08);
}
I got it from a youtube tutorial , it works but it takes alittle tweaking with the numbers to get whatever object you want to float to float just right__.__
This is the script I use to tell all the control scripts when to turn off/on … I call it PlayerSwimCommand
private var Anim : Animation;
private var Player : GameObject;
private var TPC : ThirdPersonController;
private var CC : CharacterController;
private var SC : SwimControl;
private var Swim : SwimFloat;
function Update () {
Player = gameObject;
TPC = gameObject.GetComponent(ThirdPersonController);
CC = gameObject.GetComponent(CharacterController);
SC = gameObject.GetComponent(SwimControl);
Swim = gameObject.GetComponent(SwimFloat);
Anim = gameObject.GetComponent(Animation);
}
function OnTriggerEnter(collider : Collider) {
if(collider.gameObject.tag == “Water”)
Anim.Play(“PlayerSwim”);
if(collider.gameObject.tag == “Water”)
CC.enabled = false;
if(collider.gameObject.tag == “Water”)
TPC.enabled = false;
if(collider.gameObject.tag == “Water”)
Swim.enabled = true;
if(collider.gameObject.tag == “Water”)
SC.enabled = true;
}
This script also works fine no problems…
I have the player set up like with the 3rd Person Controller, Character Controller & Third Person Camera script that comes with unity. All 3 of those scripts are turned on , PlayerSwimCommand is turned on, SwimFloat & Swimcontrol are unchecked to be off at the enter of “Game Mode”. I have a " Hitbox " attached to the player for other actions. The hitbox is just an empty game object attached as a child to a bone in the players armature. This "hitbox " is tagged “Hitbox” it has a box collider & a rigidbody attached in its components but it doesn’t interfere withh the character controller because its on a different layer and it remains unactive untill a certain “buttun press” occurs. I dont want to change anything with the “hitbox” and everything includeing the 3 scripts above work fine. You can control the 3rd person playerno probs on land and then as soon as you enter the water 3rd person controller & character controller turn off & at the same time SwimFloat & SwimControl turn on and the player is now floating and swimming on top of the water. The 3rd person camera stays on and everything works fine untill you try to get out of the water and you swim right through the shore! I cant figure out how to do the reverse for all of this so the control scripts will switch back and the player willl be back to the normal 3rd person controls on land. can anyone help me complete this very simple basic 3rd person swimming control system and keep it real simple and basic ?
I am still fairly new to scripting and not that good at it.